How to apply jQuery Validate rules to multiple fields at once?

1

I have a form in ASP.NET and I'm using jQuery Validate to apply validation rules in the fields. All my fields have a common rule ( required: true ) but I could not find a way to apply this to all at once.

$('#form1').validate({
        rules: {
            '<%= txtRazaoSocial.UniqueID %>': {
                required: true
            },
            '<%= txtNomeFantasia.UniqueID %>': {
                required: true
            },
            '<%= txtCNPJ.UniqueID %>': {
                required: true,
                cnpj: true
            },
            '<%= txtCEP.UniqueID %>': {
                required: true
            },
            '<%= txtLogradouro.UniqueID %>': {
                required: true
            },
            '<%= txtNumeroLogradouro.UniqueID %>': {
                required: true
            },
            '<%= txtBairro.UniqueID %>': {
                required: true 
[...]

Can this problem be solved?

EDIT: I tried the below as well, but it did not work.

$('#form1').validate({
    rules: {
      required: true,
    },
    
asked by anonymous 03.07.2014 / 16:04

2 answers

1

You can also try this

//Você coloca todos os campos que tiver, select, textarea etc..
$("#form1 input").each(function () {
  $(this).rules("add", {
    required: true
  });
});

Or add class="required" to the fields on your form, as stated in the comments.

    
03.07.2014 / 16:11
0

Add

class="required"

In the fields you want to make them mandatory.

And in javascript do

$(function () {
  $.validator.setValidator('idDoSeuForm');
});
    
03.07.2014 / 16:10