Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Validate - Set all fields as required by default?

I am using the jQuery Validate plugin on my form, and was wondering how I would set every field to be required by default?

I am using a custom method called 'methodname' and I tried the following code to no avail:

$.validator.setDefaults({
    methodname : true
});

Any ideas?

Thanks!

like image 918
Probocop Avatar asked Aug 30 '25 16:08

Probocop


2 Answers

Quick'n'dirty fix:

$(document).ready(function(){
    $('#myform input, #myform textarea').not([type="submit"]).addClass('required');
});
like image 54
vzwick Avatar answered Sep 02 '25 16:09

vzwick


This should do the trick, and doesn't require any alteration to the markup:

$("#myform input, #myform textarea").each(function () {
  $(this).rules("add", {
    required: true // Or whatever rule you want
  });
});

http://docs.jquery.com/Plugins/Validation/rules#rules.28.C2.A0.22add.22.2C.C2.A0rules_.29

like image 38
Alex Avatar answered Sep 02 '25 17:09

Alex