如何在jQuery验证中添加带消息的验证规则?

时间:2013-01-22 14:32:10

标签: jquery jquery-validate

我已尝试过以下代码,但无法收到错误消息。

var v = jQuery("#account_info").validate({
    //errorLabelContainer: $("#result"),
    submitHandler: function(form) {
        jQuery(form).ajaxSubmit({                               
           target: "#checkOut_error",                   
            success: function(msg) {
                //setTimeout("window.location='MyBids.php'", 2000);             
                if(msg == '<?php echo OBJECT_STATUS_SUCCESS;?>')    {
                    $('#checkOut_error').html('<div class="msg msg-thanks">Bid Submitted Successfully !</div>');
                    //setTimeout("window.location='"+<?php echo LINK_TO_TENBID;?>+"'", 2000);
                    //setTimeout("window.location.reload(true)",2000);
                   //$('.result').html('<div class="msg msg-thanks">Bid Submitted Successfully !</div>');

                } else{
                                        $("#checkOut_error").html(msg);
                                    }
            },
            clearForm:  false,
            resetForm:  false
        });
    },
           errorLabelContainer: "#checkOut_error",
          rules: {
                phone_number: {
                    required: true

                },
                recipient_name: {
                    required: true,
                    min_length: 6
                }

          },
          messages: {
                recipient_name: {
                    required: "Enter recipient name",
                    min_length: "Name should be atleast 6 characters long"
                }
          }
 });

- 如何添加规则和错误消息?

- 验证插件使用哪个属性:name或id?

- 如何一次显示一条错误消息?

2 个答案:

答案 0 :(得分:8)

  

- 验证插件使用哪个属性:name或id?

As per documentation,该插件要求所有input字段都包含name attribute。在rules内指定messages.validate()时,您可以使用name attribute

  

- 如何添加规则和错误消息?

min_length规则应包含下划线。它是minlength。否则,您的代码看起来是正确的。

基于您的代码的简单演示...

<强> jQuery的:

$(document).ready(function () {

    $("#account_info").validate({
        rules: {
            phone_number: {
                required: true
            },
            recipient_name: {
                required: true,
                minlength: 6  // <-- removed underscore
            }
        },
        messages: {
            phone_number: {
                required: "this field is required"
            },
            recipient_name: {
                required: "Enter recipient name",
                minlength: "Name should be at least {0} characters long" // <-- removed underscore
            }
        },
        submitHandler: function (form) { // for demo
            alert('valid form');  // for demo
            return false;  // for demo
        }
    });

});

<强> HTML:

<form id="account_info">
    <input type="text" name="phone_number" />
    <br/>
    <input type="text" name="recipient_name" />
    <br/>
    <input type="submit" />
</form>

正在使用jsFiddle演示:http://jsfiddle.net/rfgRL/


此外,使用minlength之类的规则时,您的自定义消息可以包含插入特定规则定义的占位符{0}

minlength: 6

带有自定义消息......

minlength: "Name should be at least {0} characters long"

会自动显示......

  

名称长度至少为6个字符


由于您只在DOM上调用.validate() 一次准备仅使用其选项初始化验证插件,因此将其分配给变量没有任何意义;它不会在任何地方重复使用......至少它不应该是。

var v = jQuery("#account_info").validate({...});

简直就是这个......

jQuery("#account_info").validate({...});

在您的代码中,如果您需要测试form的有效性或重新触发有效性测试,use the .valid() method。它将返回任何挂起的表单错误的布尔值和触发器显示。例如:

if (jQuery("#account_info").valid()) { ...

答案 1 :(得分:0)

这是一个简单的jquery注册(注册)验证器:

        $('#submitButton').click(function() {
        $("#accounterForm").validate({
    rules: {
        firstName: "required",
        lastName: "required",
        companyName: {
            required: true,
            no_special_characters: true,
            maxlength: 64
        },
        companyFullName: "required",
        password: {
            required: true,
            minlength: 6
        },
        confirmPassword: {
            required: true,
            minlength: 6,
            equalTo: "#mid-box4"
        },
        emailId: {
            required: true,
            email: true
        }
    },
    messages: {
        firstName: "Please enter your first name",
        lastName: "Please enter your last name",
        companyName: "Please enter company ID",
        companyName: {
            required: "Please enter company ID",
            no_special_characters: "Company ID should contain atleast one letter and no special characters",
            maxlength: "Company ID exceeded the maximum length"
        },
        companyFullName: "Please enter company name",
        password: {
            required: "Please provide a password",
            minlength: "Your password must be at least 6 characters long"
        },
        confirmPassword: {
            required: "Please provide a password",
            minlength: "Your password must be at least 6 characters long",
            equalTo: "Please enter the same password as above"
        },
        emailId: "Please enter a valid email address",
        confirmemailId:{
        required:"Please confirm email address",
        equalTo :"Emailid and confirm emaild must be same"
        },
        agree: "Please accept Terms of use"
    }
});