帮助jQuery验证

时间:2010-11-04 12:09:35

标签: javascript jquery

我在编写表单验证时遇到问题。我将在下面发布代码。基本上,如果cboAction在另一个选择上,我希望表单提交。

cboAction有三个值, [Select] 插入搜索。因此,如果在组合框中选择了Insert,那么如果选择了Search,则表单应使用jquery else进行验证,然后表单应该被提交。我尝试在那里放一个if语句,但它没有用。 :(

顺便说一下表单验证工作,它只是我无法完成上述工作。希望收到某人的来信。

由于

jQuery.validator.addMethod(
            "selectOption",
            function(value, element) {
                if(element.value == "nothing") {
                    return false;                       
                } else {
                    return true;
                }
            },
            "Please select an option."
        );

        $(document).ready(function() {              
            $("#authorForm").validate({                 
                rules: {                                                        
                        cboAction: {selectOption:false}
                        txtName: {required:true},
                        txtStreet: {required:true}  
                        txtTown: {required:true},
                        cboState: {selectOption:true},
                        txtPostcode: {required:true},
                        txtMobile: {required:true},
                        txtEmail: {required:true, email:true}                               
                },
                messages: {
                    cboAction: "Please select an action"                                                        
                    txtName: {required: "Author's Name is a required field"},
                    txtStreet: {required: "Street is a required field"}                         
                    txtTown: "Town is a required field",
                    cboState: "Please select a state",
                    txtPostcode: "Postcode is a required field",
                    txtMobile: 
                    {
                        required: "Mobile is a required field",
                        email: "Please enter a valid email adress"
                    }
                }           
            });
        });

2 个答案:

答案 0 :(得分:1)

您必须手动触发验证:

http://docs.jquery.com/Plugins/Validation/Validator/form

首先阻止提交验证:

$("form").validate({
    onsubmit: false
});

添加提交处理程序以测试cboAction。

$('form').submit(function(event){
    if ($('#cboAction').val() == 'insert' && !$('form').validate().form()){
        return false;
    }
    return true;
});

答案 1 :(得分:0)

这就是它的工作原理。

        jQuery.validator.addMethod(
            "selectOption",
            function(value, element) {
                if(element.value == "nothing") {
                    return false;                       
                } else {
                    return true;
                }
            },
            "Please select an option."
        );

        $(document).ready(function() {                              

            $("#authorForm").submit(function(event){
                if($("#cboAction").val() == "insert" && !$("#authorForm").validate().form()){
                    return false;
                }
                return true;
            });

            $("#authorForm").validate({
                onsubmit: false,
                rules: {                                                        
                        //cboAction: {selectOption:false},
                        txtName: {required:true},
                        txtStreet: {required:true}, 
                        txtTown: {required:true},
                        //cboState: {selectOption:true},
                        txtPostcode: {required:true},
                        txtMobile: {required:true},
                        txtEmail: {required:true, email:true}                               
                },
                messages: {
                    //cboAction: "Please select an action",                                                 
                    txtName: {required: "Author's Name is a required field"},
                    txtStreet: {required: "Street is a required field"},                    
                    txtTown: "Town is a required field",
                    //cboState: "Please select a state",
                    txtPostcode: "Postcode is a required field",
                    txtMobile: "Mobile is a required field",
                    txtEmail:
                    {
                        required: "Email is a required field",
                        email: "Please enter a valid email adress"
                    }
                }           
            });
        });