0

I have a form with large set of fields that includes both read only and hidden fields. I want to enable validation for hidden field but disable validation for ready only fields

In the new version of jQuery validation plugin by the default validation of hidden fields are ignored. So to enable it I have to use :

$("form[name='Formname']").validate({
 ignore: []
});

and it is working fine. But I need to ignore the validation for readonly fields and for that I need to use

$("form[name='Formname']").validate({
 ignore: [readonly=readonly]
});

Even if i merge the two,it still doesn't work because than it only ignore readonly but doesn't apply vaildation on hidden field

1 Answers1

1

You have to pass selector to ignore like this

ignore: '[readonly]', // This will select all input with readonly and ignore

Elements to ignore when validating, simply filtering them out. jQuery's not-method is used, therefore everything that is accepted by not() can be passed as this option. Inputs of type submit and reset are always ignored, so are disabled elements.

SNIPPET

$("form[name='Formname']").validate({
 ignore: '[readonly]',
 rules: {
    field: {
      required: true
    },
    field1: {
      required: true
    }} 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>

<form name="Formname">

<input readonly name="field1">
<input type="text" id="field" name="field">

<input type="submit" value="Validate!">

</form>
Nadir Laskar
  • 4,012
  • 2
  • 16
  • 33