spyder
spyder

Reputation: 330

How can i validate number of words?

I am using this plugin. Is there any way to validate number of words?

For eg in "firstname" there shouldn't be any spaces allowed. So, the validating criteria shud be like this => maxwords="1"

Is there any way to do this currently? If not, what should i do to get this going.

Thanks

Upvotes: 0

Views: 642

Answers (1)

Sparky
Sparky

Reputation: 98738

Use the addMethod() method to create your custom rule.

Example:

jQuery.validator.addMethod("maxwords", function(value, element, params) {
    // your function to count words goes in here
    // use any of these arguments in your function: value, element, params
    // value => the present value of the field being tested
    // element => the present field being tested
    // params => the parameters passed in when you declare the rule.  
    // example:  // maxwords: 1  // params[0] would be 1
    // return true // if the field passes validation
    // return false // if the field fails validation and the message below will display
}, "Please enter at least {0} words."));

Upvotes: 2

Related Questions