0

Using jQuery Validate, I am doing a conditional validation on a select and a text field: the text field (#specialization) must be filled if a particular option (<option value="doctor">) was choosen from the select field (#job)

So far so good, here is a working copy: https://jsfiddle.net/vzx9paxz/2/

Now, I don't like to mess with too much JS code and I'd like to take advantage of the data-rule-required attribute. So, I'd like to evaluate the expression ($('#job').val() === 'doctor') directly in the HTML code, removing it from the Javascript: if it returns true, the #specialization field must be filled, otherwise it can be empty

Something like this:

<input type="text" data-rule-required="return ($('#job').val() === 'doctor')" class="form-control" id="specialization" name="specialization">

Of course this code doesn't work: I tried other expressions, like javascript:return ($('#job').val() === 'doctor') but with no luck at all...

0

3 Answers 3

1

How to: Use data attribute for conditional validation

You can't.


data-rule-required="return ($('#job').val() === 'doctor')"

Even if you could execute JavaScript inside of a data attribute, which you can't, the jQuery Validate plugin cannot interpret it (see the JS errors in the console). Normally, it would be looking for a true or false string within data-rule-required... but it cannot execute a conditional expression.

In other words, no matter what you put as a value for data-rule-required, the jQuery Validate plugin will interpret it as a string and choke on anything other than "true" or "false".

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

1 Comment

Thanks, got it! :-)
0

Data attribute cant trigger event. Better write a javascript function and call it with a click, focus, resize, mouseup...event. Like:

 <input class="user" name="">

 <script>
  $(".user").focus(function(){
 if($(this).val()=="doctor"){
 // do what ever
  }
  });

1 Comment

You're right about the data attribute. However, the OP does not need to write an event handler, because the conditional can be placed within the plugin's .validate() method.
0

What OP asks is not possible in general. However in some simple cases, conditional validation is possible using exclusively data attributes. Here's an example:

<form action="#" method="post">
    <label for="password">Set new password</label>
    <input type="password" id="password" name="password">
    <br>
    <label for="password-confirm">Confirm password</label>
    <input type="password" id="password-confirm"
        data-rule-equalto="#password"
        data-rule-required="#password:filled">
    <br>
    <input type="submit" value="save">
</form>

See it live: JSFiddle

It will not require either password fields, unless the first field is non-blank, then the second field will be required. This works thanks to the extra selectors provided by jQuery Validate.

Other useful selectors: :checked, :unchecked, :blank.

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.