简体   繁体   English

输入有效输入并提交表单时,其余的输入字段为空,JS验证不会运行

[英]JS validation doesn't run after I enter a valid input and submit the form with rest of the input fields empty

I am developing a web directory with a form on one of its pages. 我正在开发一个网页目录,其中一个页面上有一个表单。 JS validation plug in is used. 使用JS验证插件。 While submitting the form without filling any Input fields, the form throws errors below each input field as expected! 在提交表单而不填写任何输入字段时,表单会按预期在每个输入字段下面抛出错误! But submitting the form with just one input box filled in refreshes the current page as the action value is set to current page with PHP codes in it, instead of staying on the same page to continue to throw errors for the rest of the fields that are yet to be filled in! 但是只用一个输入框提交表单就会刷新当前页面,因为操作值设置为当前页面,其中包含PHP代码,而不是停留在同一页面上继续为其余字段抛出错误尚未填写! I have searched online to find nothing useful in figuring out what is wrong with the script. 我在网上搜索,发现没有什么用于弄清楚脚本有什么问题。 Could anyone here please look into the code below and recommend the best solution? 请问这里有人请查看下面的代码并推荐最佳解决方案吗? Thanks. 谢谢。

$(document).ready(function()  {
    $("#userForm").validate({
        rules: {
            cname: {
                required: true,
                lettersonly: true,
                minlength: 3
            },
            cemail: {
                required: true,
                email: true
            },
            cphone: {
                required: true,
                number: true,
                minlength: 10,
                maxlength: 10
            },
            cbusiness: {
                required: true,
                url: true
            },
            cbcategory: {
                required: true,
                minlength: 6
            },
            curl: {
                required: true,
                minlength: 6
            },
        },
        messages: {
            cname:  "Please enter your name",
            cemail: "Please enter a valid email address",
            cphone: {
                required: "Please enter your phone number",
                number:   "Please enter only numeric value"
            },
            cbusiness: {
                required: "Please enter your business",
                },
            cbcategory: {
                required: "Please enter a business category",
                },
            curl: {
                required: "Please enter the URL to your website",
                },
            }
    });
});

The form is as below. 表格如下。

<form action="" method="post" name="userForm" id="userForm">

                            <input type="text" name="cname"  id="cname" placeholder=" Your Name">
                            <input type="text" name="cemail" id="cemail" class="email" placeholder="Your Email">
                            <input type="text" name="cphone" id="cphone" placeholder="Your Phone">
                            <input type="text" name="cbusiness"  id="cbusiness" class="email" placeholder=" Your Business">
                            <input type="text" name="cbcategory" id="cbcategory" placeholder="Business category">
                            <input type="text" name="curl" id="curl" class="email" placeholder="URL"><br>       

                            <label for='message'>Enter the code in the box below : </label>
                            <img src="captcha.php?rand=<?php echo rand();?>" id='captchaimg'>
                            <input type="text" id="captcha_code" name="captcha_code">

                            <input type="submit" name="Submit" id="Submit"  value="Submit" class="button1"><br>
                Can't read the image? click <a href='javascript: refreshCaptcha();'>here</a> to refresh.
                        </form>

Assuming you have included the validation libraries corectly, you will have to set messages for all of the validation types like: 假设您已经核心地包含了验证库,则必须为所有验证类型设置消息,例如:

messages: {
  cname: {
     required: "Please enter your name",
     lettersonly: "Letters only",
     minlength: "Min length 3 required"
  },
  cemail: {
    required: "Please enter a valid email address",
    email: "Invalid email"
  }
}

Working JSFIDDLE . 正在使用JSFIDDLE

You have to include the plugin files something like this: 你必须包含这样的插件文件:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/additional-methods.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/jquery.validate.js"></script>

UPDATE : 更新

After discussing with OP on chat, we came to the conclusion that the plugin files had to be included correctly and there was an incompatibility between the jQuery version(v 1.7.1) he used and the plugin version(v 1.16.0). 在与OP讨论聊天后,我们得出结论,插件文件必须正确包含,并且他使用的jQuery版本(v 1.7.1)和插件版本(v 1.16.0)之间存在不兼容性。 So we had to add a custom method for lettersonly . 因此我们必须为lettersonly添加自定义方法。

The code for custom method: 自定义方法的代码:

jQuery.validator.addMethod("lettersonly", function(value, element) {
  return this.optional(element) || /^[a-z]+$/i.test(value);
}, "Letters only please");

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

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