我尝试执行jQuery验证代码时收到错误

时间:2013-06-02 04:25:13

标签: jquery jquery-validate

在单个页面上使用多形式验证Clay Campbell从jsfiddle和Stackoverflow借用了重大修改5-28-2013    这是使用jQuery Validate liberary Murach jQuery Book也被使用

$(document).ready(function () {

    $.validator.setDefaults({
        //debug: true, // blocks submit
        errorElement: 'span', //default input error message container
        errorClass: 'help-inline', // default input error message class
        focusInvalid: false, // do not focus the last invalid input)
        highlight: function (element) { // hightlight error inputs
            $(element).closest('.control-group').addClass('error'); // set error class to the control group
        },
        unhighlight: function (element) { // revert the change done by hightlight
            $(element).closest('.control-group').removeClass('error'); // set error class to the control group
        },
}); // end ready function

// init validator obj and set the rules for registrationForm
$('#registrationForm').validate({
    rules: {
        email: {
            required: true,
            email: true
        },
        password: {
            required: true,
            minlength: 6
        },
        verify: {
            required: true,
            equalTo: "#password"
        },
        firstName: {
            required: true
        },
        lastName: {
            required: true
        },
        address: {
            required: true
        },
        city: {
            required: true
        },
        state: {
            required: true,
            rangelength: [2, 2]
        },
        zip: {
            required: true,
            rangelength: [5, 10]
        },
        phone: {
            required: true,
            phoneUS: true
        }
    }
}); // end jQuery validation method call for registrationForm

// init validator obj and set the rules rules for memberForm
$('#memberForm').validate({
    rules: {
        emailMem: {
            required: true,
            email: true
        },
        passwordMem: {
            required: true,
            minlength: 6
        }
    }
}); // end jQuery validation method call for memberForm

2 个答案:

答案 0 :(得分:1)

下面是演示应用程序,一看看。我希望它会对你有所帮助。

https://github.com/diatmpravin/jquery-validation.git

答案 1 :(得分:0)

您有语法错误,如下所示......

$(document).ready(function () {

    $.validator.setDefaults({
        //debug: true, // blocks submit
        errorElement: 'span', //default input error message container
        errorClass: 'help-inline', // default input error message class
        focusInvalid: false, // do not focus the last invalid input)
        highlight: function (element) { // hightlight error inputs
            $(element).closest('.control-group').addClass('error'); // set error class to the control group
        },
        unhighlight: function (element) { // revert the change done by hightlight
            $(element).closest('.control-group').removeClass('error'); // set error class to the control group
        },  // <-- REMOVE THIS TRAILING COMMA
    });     // <-- ADD THIS LINE

// }); // end ready function  // <-- MOVE THIS LINE TO END

    // init validator obj and set the rules for registrationForm
    $('#registrationForm').validate({
        rules: {
            email: {
                required: true,
                email: true
            },
            password: {
                required: true,
                minlength: 6
            },
            verify: {
                required: true,
                equalTo: "#password"
            },
            firstName: {
                required: true
            },
            lastName: {
                required: true
            },
            address: {
                required: true
            },
            city: {
                required: true
            },
            state: {
                required: true,
                rangelength: [2, 2]
            },
            zip: {
                required: true,
                rangelength: [5, 10]
            },
            phone: {
                required: true,
                phoneUS: true
            }
        }
    }); // end jQuery validation method call for registrationForm

    // init validator obj and set the rules rules for memberForm
    $('#memberForm').validate({
        rules: {
            emailMem: {
                required: true,
                email: true
            },
            passwordMem: {
                required: true,
                minlength: 6
            }
        }
    }); // end jQuery validation method call for memberForm

}); // end ready function // <-- MOVED TO END