JQuery验证器没有验证表单

时间:2015-03-25 19:03:02

标签: jquery regex jquery-validate

注意regex内容是实验性的,我已经尝试过但仍然无效,如果可能,有人可以告诉我它是否有任何问题吗?

我看不出我在这里做错了什么。还有其他功能在document.ready中正确显示,所以我认为也不是。

我的代码:

function checkForm() {
$.validator.addMethod("regex", function(value, element, regexpr) {          
    return regexpr.test(value);
} );
$('#contactform').validate( { 
    rules: { 
        first: {
            required: true,
            minLength: true,
            regex: /^[A-z]{2,}$/
        },
        last: {
            required: true,
            minLength: true,
            regex: /^[A-z]{2,}(-[A-z]{2,})?$/
        },
        titledd: 'required',
        zhanum: {
            required: true,
            regex: /^(ZHA|zha)([0-9]{6})$/
        },
        emailadd: { 
            required: true,
            email: true
        },
        telephone: {
            regex: /^[0-9]{5,16}$/
        }
    },
    messages: {
        first: {
            required: "You must enter a first name",
            minLength: "Your first name must have more than 2 characters",
            regex: "Please enter a first name with more than two characters, where only letters are allowed"
        },
        last: {
            required: "You must enter a last name",
            minLength: "Your last name must have more than 2 characters",
            regex: "Please enter a last name with more than two characters, where only letters and optionally one hyphen are allowed"
        },
        titledd: {
            required: "Please select your title"
        },
        zhanum: {
            required: "You must enter your ZHA number",
            regex: "Your ZHA number should be ZHA followed by six numbers only"
        },
        emailadd: { 
            required: "You must enter an email address",
            email: "Please enter a valid email address"
        },
        telephone: {
            regex: "Please enter a telephone number with more than five characters and less than 16 characters, where only numbers are allowed"
        }
    }
} );
}

$(document).ready(function() {
    checkForm();
} );

2 个答案:

答案 0 :(得分:0)

试试这个:

$.validator.addMethod("regex", function(value, element, regexpr) {          
    return regexpr.test(value);
} );
$('#contactform').validate( { 
    rules: { 
        first: {
            required: true,
            minLength: true,
            regex: /^[A-z]{2,}$/
        },
        last: {
            required: true,
            minLength: true,
            regex: /^[A-z]{2,}(-[A-z]{2,})?$/
        },
        titledd: 'required',
        zhanum: {
            required: true,
            regex: /^(ZHA|zha)([0-9]{6})$/
        },
        emailadd: { 
            required: true,
            email: true
        },
        telephone: {
            regex: /^[0-9]{5,16}$/
        }
    },
    messages: {
        first: {
            required: "You must enter a first name",
            minLength: "Your first name must have more than 2 characters",
            regex: "Please enter a first name with more than two characters, where only letters are allowed"
        },
        last: {
            required: "You must enter a last name",
            minLength: "Your last name must have more than 2 characters",
            regex: "Please enter a last name with more than two characters, where only letters and optionally one hyphen are allowed"
        },
        titledd: {
            required: "Please select your title"
        },
        zhanum: {
            required: "You must enter your ZHA number",
            regex: "Your ZHA number should be ZHA followed by six numbers only"
        },
        emailadd: { 
            required: "You must enter an email address",
            email: "Please enter a valid email address"
        },
        telephone: {
            regex: "Please enter a telephone number with more than five characters and less than 16 characters, where only numbers are allowed"
        }
    }
} );

并确保在<form>代码

之后保留它

答案 1 :(得分:0)

我发现了问题,在我的HTML中我有id个属性而不是name属性,我没有意识到这是一个问题。