undefined is our god
undefined is our god

Reputation: 511

What's the best way to validate an input field?

Let's put the following situation:

I have a form which has an input field that need to be validated. I know 2 ways of doing this:

1 - Calling the method with all the rules included on the call:

$("form").validate({ 
  rules: {
    'field1': {
       required: true,
    }
  }
});

2 - Setting the HTML field as follows:

<input type="text" data-val-required="true"/>

... and calling the method...

$("form").validate();

My question is: Which one of the above rules is the best for validating (considering performance and browser compatibility (if there's any restriction))? Or there's no difference?

Upvotes: 0

Views: 64

Answers (2)

Sparky
Sparky

Reputation: 98738

AFAIK, it makes no difference. However, it's a moot point since the .validate() method (plugin initialization) is only called once when the page loads. After the page loads, the plugin handles validation automatically and how rules were declared wouldn't matter at that point.

Upvotes: 1

jdunham
jdunham

Reputation: 439

In my experience for basic validation there is not much of a difference. However using the method call allows to use custom rules or base requirements on other form elements state, etc.

Upvotes: 1

Related Questions