0

I have implemented validation using jquery validate plugin and trying to apply in the following ways

first added $.validator.addMethod("character" ........);

and executing in below ways

1. Will apply Rules for that particular field

 rules :{
        someName: {
                    character: true,
                    required: true,
                },
        }

2. Will apply at class level

// connect it to a css class
    $.validator.addClassRules({
        character: { character: true }
    });

What am looking for is is there a way i can make this validation code for all input Textboxes in a small line rather than adding class for all textboxes !

What will be the best way to implement?

As i want this validation at application level am looking for some easy approach rather touching every where!

Thanks

user2067567
  • 3,695
  • 15
  • 49
  • 77

2 Answers2

2

Yes, the .addClassRules() method will create a new custom class that you can add to any input element for compound rule assignment. This is a valid approach.

HOWEVER, there is another equally valid approach to solve your issue without having to add new classes, or anything else, to your HTML markup. Use the plugin's .rules('add') method enclosed within a jQuery .each() and a jQuery selector to target all input elements that are type="text".

$('input[type="text"]').each(function() {
    $(this).rules('add', {
        character: true,
        required: true
    });
});

Working DEMO: http://jsfiddle.net/rW8Zd/1/

Also see this answer for more ways to apply rules.

Community
  • 1
  • 1
Sparky
  • 98,165
  • 25
  • 199
  • 285
-1

Actually the answer is no!

As I want this validation at application level am looking for some easy approach rather touching every where!

Since you have done validation using jQuery.validator.addClassRules(), there is an easy to add class and then approach validation.

$('input type["text"]').addClass('className');

Now the class is added to all elements of input type="text".

Hope you understand.

Praveen
  • 55,303
  • 33
  • 133
  • 164