如何包含 jquery.validate.js 文件(在本地目录中)并在表单验证中使用它
How to include jquery.validate.js file (in the local directory) and use it in the validation of form?
我知道包括<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>
就可以了,但我无法在任何地方找到下载文件并将其粘贴到本地目录后如何完成jquery.validate.js
。
我通过将jquery.validate.js
文件粘贴到与验证文件相同的目录中并使用 : <script src="jquery.validate.js"></script>
来尝试,但它不起作用。
验证代码位于 register.phtml 文件中,其脚本部分为:
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>
<script>
$(document).ready(function () {
// validate signup form on keyup and submit
$("#registerForm").validate({
rules: {
first_name: "required",
last_name: "required",
contact_no: {
required: true,
digits: true,
minlength: 10,
maxlength: 13,
},
email: {
required: true,
email: true,
},
invitation_code: {
required: true,
},
},
messages: {
first_name: "Please enter your First Name",
last_name: "Please enter your Last Name",
contact_no: {
required: "Please enter your Contact Number",
digits: "Please enter only digits(0-9)",
minlength: "Minimum 10 digits are required",
maxlength: "Maximum 13 digits are allowed",
},
email: {
required: "Please enter a valid Email address",
email: "Your email address must be in the format of name@domain",
},
invitation_code: {
required: "Please check your email for Invitation Code",
},
}
});
$("#email").keypress(function () {
$('.error_msg1').hide();
});
$('#invitationCode').click(function () {
document.getElementById('invitation').innerHTML = "";
});
$("#first_name").on('keyup', function(e) {
var val = $(this).val();
if (val.match(/[^a-zA-Z]/g)) {
$(this).val(val.replace(/[^a-zA-Z]/g, ''));
}
});
$("#last_name").on('keyup', function(e) {
var val = $(this).val();
if (val.match(/[^a-zA-Z]/g)) {
$(this).val(val.replace(/[^a-zA-Z]/g, ''));
}
});
$('#submitform').click(function () {
if (!$('#Check_TermsAndCondidation').prop('checked'))
{
$('#Check_TermsAndCondidation_msg').html('Please check Terms and Conditions');
return false;
}
});
$('#Check_TermsAndCondidation').change( function(){
$('#Check_TermsAndCondidation_msg').html('');
});
});
</script>
上面的代码片段正在正确验证,但在将上述代码的第一行编辑为: <script src="jquery.validate.js"></script>
,它不起作用。
问题几乎可以肯定是这样的:你把你的文件放在了错误的目录中。
假设您的网站使用的是PHP(很可能)和Apache,则jquery.validate.js
文件的位置应相对于服务器上网站的根文件夹。那是与 localhost
.这通常是/var/www
,但这实际上取决于您的服务器设置。
假设您使用的是 PHP,则文件位置需要相对于的确切文件夹可能包含一个名为 index.php
的文件和一个名为 .htaccess
的文件。
如果服务器上网站的根文件夹是 /var/www
,您可能还应该看到一个名为 /var/www/js
的文件夹。在这种情况下,正确的做法是将jquery.validate.js
文件放在文件夹中/var/www/js
并将其添加到register.phtml
文件中,如下所示:
<script type="text/javascript" src="/js/jquery.validate.js"></script>
如果您对这些步骤中的任何一个感到困惑,请在评论中告诉我,我将尝试进一步帮助您完成您可能感到困惑的任何步骤。
这应该有效:
- 转到此页面
- 选择所有代码
- 复制该代码
- 在 HTML 文件的同一目录中创建一个名为
jquery.validate.js
的文件,并将代码粘贴到该目录中 - 将此脚本标记放在您的 HTML 文件中,其中包含您创建的文件的名称:
<script type="text/javascript" src="jquery.validate.js"></script>
只需创建一个js文件夹,该文件夹将保存所有javascript文件。将jquery.validate.js
放在该目录中。现在,将其包含在项目中取决于您使用的框架/MVC系统。假设您正在使用 laravel MVC,然后将其用作
<script src="{{ Request::root() }}/js/jquery.validate.js"></script>
如果使用简单的 php,请使用 echo $_SERVER['SERVER_NAME'];
访问它基本上,您必须使用项目基 url 来包含文件。希望对您有所帮助。
- 解析javascript表单验证器
- Jquery表单验证插件-如果选中复选框,如何在提交时执行某些操作
- 自定义表单验证和提交
- Javascript表单验证的缺点
- Jquery的表单验证不起作用
- 语义ui表单验证:图像url
- jQuery表单验证适用于Mozilla和Internet Explorer,但不适用于Chrome或Safari
- JavaScript表单验证-“;“错误”;不按需要工作
- Mailchimp内联表单验证不适用于JS加载的HTML
- jQuery/JS获胜't在表单验证后重定向
- jquery表单验证复选框错误显示
- javascript表单验证-数值和密码
- 我想知道如何在javascript中进行表单验证
- jQuery表单验证(如果==“执行此操作”)仅适用于第一次输入
- 使用记录保存的消息进行表单验证
- 表单验证工作不正常,在不检查条目的情况下继续
- 如何对未知表单元素进行表单验证
- 使用ajax、php和mysql进行表单验证
- JS表单验证未发生
- 表单验证jsphp路径