0

在单个页面上使用多表单验证 Clay Campbell 从 jsfiddle 和 Stackoverflow 借来并进行了重大修改 5-28-2013 这是使用 jQuery Validate 库,还使用了 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
4

2 回答 2

1

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

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

于 2013-06-02T05:54:55.337 回答
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
于 2013-06-02T17:02:00.570 回答