表单验证错误 - 等于

时间:2013-09-06 12:15:39

标签: jquery jquery-validate

我使用jQuery验证库来验证表单。出于某种原因,当我尝试添加confirm-email时出错。错误是意外的令牌 -

HTML中的ID是正确的。

var validation_options = {
focusInvalid: false,

rules: {
    name:           { required: true },
    houseno:        { required: true },
    streetname:     { required: true },
    postcode:       { required: true, postcodeUK: true },
    country:        { required: true },
    phone:          { required: true, validPhone: true },
    email:          { required: true, email: true, unique: true },
    confirm-email:  { required: true, equalTo: "#email" }
},


messages: {
    name:           "Please enter your name",
    houseno: {
        required:   "Please enter your house number",
    },
    streetname:     "Please enter your street name",

    postcode: {
        required:   "Please enter your postcode",
        postcodeUK: "Please enter a valid postcode"
    },

    country:        "Please enter your country",

    phone: {
        required:   "Please enter your mobile number",
        validPhone: "Please enter a valid phone number"
    },
    email: {        
        required:   "Please enter your email",
        email:      "Please enter a valid email address",
        unique:     "This email has already been used"
    },

    confirm-email: {
        required: "Please enter your email"
    }   

},

2 个答案:

答案 0 :(得分:2)

破折号是变量名称的非法字符,它将被解释为减号。您必须将confirm-email更改为confirm_email或其他内容。

答案 1 :(得分:0)

目前接受的答案只有一半是正确的。是的,短划线,-,括号,[]或点.等字符将成为此插件的问题。但是, 不是 ,需要将confirm-email更改为confirm_email或其他内容。

您只需将名称括在引号中。

rules: {
    'confirm-email':  {
        required: true,
        equalTo: "#email"
     }
}

请参阅the "Fields with complex names (brackets, dots)" section on the "General Guidelines" page of the documentation

工作演示http://jsfiddle.net/Zaf8N/