0

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

Community
  • 1
  • 1
spyder
  • 330
  • 3
  • 6
  • 18

1 Answers1

2

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."));
Sparky
  • 98,165
  • 25
  • 199
  • 285