1

I wanted to add conditional rules dynamically on the page load here is my code what I have tried I have created jsfiddle First code sample

 $("input[name$='DistanceToRespondingFireDept']").rules("add", {
        required: {
            depends: function (element) {
                return $("input:radio[name='DistanceToNearestFireHydrnt']:checked").val() == 'U';
            }
        },
        messages: {
            required: "Tested"
        }
    });

second code

$("input[name$='DistanceToRespondingFireDept']").rules("add", {
        required: function (element) {
            return $("input:radio[name='DistanceToNearestFireHydrnt']:checked").val() == 'U';
        },
        messages: {
            required: "Tested"
        }
    });

I get an error "cannot call method call of undefined"

If I don't add any conditions do just simple required: true it works

Any suggestion how to fix

2
  • You don't need <html>, <head> or <body> tags in your jsFiddle; just insert the HTML that's between your <body></body>. Commented Sep 18, 2013 at 15:16
  • You should also show your full code example in the OP. Otherwise, when the jsFiddle dies, the question will not be as helpful to others. Commented Sep 18, 2013 at 15:37

1 Answer 1

1

You have a syntax error.

The opening brace, {, was missing from here...

if (test === "a") { // <-- this opening brace was missing

Your DEMO: http://jsfiddle.net/TYbS5/4/

You should also update your jQuery Validate plugin to version 1.11.1, which is currently the latest.

And finally, both of your buttons are type="submit" which means the plugin will think they're both for submitting the form. You should change the "CLEAR" button into type="button" in order to prevent the plugin from capturing its click event.


EDIT:

As per comments, here is code for conditional validation...

required: function() {
    return $('#DistanceToNearestFireHydrnt1').is(':checked');
},

Working DEMO: http://jsfiddle.net/TYbS5/5/

Sign up to request clarification or add additional context in comments.

3 Comments

I had updated the JS fiddle but forgot to update it in the question for { error. Secondly this is a dummy form created for testing So have not changed the button type a simple copy paste :-(
@diveshsalian, So what's the problem then? Your code is working without error as demonstrated by my jsFiddle.
I want to add conditional validation not just required:true I have shared the code sample in question i.e required if selected radiobutton value is U

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.