开发者

Help with jQuery validation

开发者 https://www.devze.com 2023-01-23 06:19 出处:网络
I\'m having a problem coding for a form validation. I\'ll post the code below. Basically i want the form to submit if cboAction is on another selection.

I'm having a problem coding for a form validation. I'll post the code below. Basically i want the form to submit if cboAction is on another selection.

cboAction has three values, [Select], Insert and Search. So if Insert is selected in the combobox then the form should validate using jquery else if Search is selected then the form should get submitted. I tried putting an if statement in there but it didn't work. :(

Btw the form validation works, its just that i can't get the above working. Hope to hear from someone.

Thanks

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"
                    }
                }           
            });
        });


You will have to trigger the validation manually:

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

First prevent validation on submit:

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

Add submit handler to test cboAction.

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


This is what made it work.

        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"
                    }
                }           
            });
        }); 
0

精彩评论

暂无评论...
验证码 换一张
取 消