0

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

2 Answers 2

1

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;
});
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.