如何包含 jquery.validate.js 文件(在本地目录中)并在表单验证中使用它

How to include jquery.validate.js file (in the local directory) and use it in the validation of form?

本文关键字:表单 验证 validate jquery 何包含 js 文件      更新时间:2023-09-26

我知道包括<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>

如果您对这些步骤中的任何一个感到困惑,请在评论中告诉我,我将尝试进一步帮助您完成您可能感到困惑的任何步骤。

这应该有效:

  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']; 访问它基本上,您必须使用项目基 url 来包含文件。希望对您有所帮助。