繁体   English   中英

如何包括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(最有可能是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>

如果您对这些步骤中的任何一个感到困惑,请在评论中让我知道,我们将尽一切可能使您感到困惑的步骤来帮助您。

这应该工作:

  1. 转到此页面
  2. 选择所有代码
  3. 复制该代码
  4. 在HTML文件的同一目录中创建一个名为jquery.validate.js的文件,并将代码粘贴到此处
  5. 将此脚本标签与您创建的文件的名称一起放在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'];进行访问echo $_SERVER['SERVER_NAME']; 基本上,您必须使用项目基本URL来包含文件。 希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM