1

I"m using Jquery form validation which in the basic form looks like this

$('#shopping_form').validate({          
  submitHandler: function(){
  $.post("/url", { message: messageval}, function(data) {
  ......
  ......
  },
}
,rules: {
    message: { required:true },
    price: { number: true  }                
}
});

Now I am adding two radio buttons to this form call them radio1 and radio2. If radio1 is selected then the field message is required and for radio2 its not a required field.

How can I add this kind of rule. Thanks for your help in advance

1 Answer 1

1

You can do this by writing a custom method which checks radio button value and depending on that value , it checks for message field. Here is the code you can add in jQuery

$.Validation.addRule("message",{
 check: function(value) {
    // Check the value of radio button here, You can do it as 
      if($("form input:radio:checked").val() == 'radio2') return true;
     // radio2 is checked then return valid, ie. this field is not required field.
    if(value.length > 0) {
        return true;
    }
    return false;
},
msg : "Field is required."
});
Sign up to request clarification or add additional context in comments.

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.