I want to apply to the field check through the pattern. Something like this:

$(document).ready(function(){ $("#registration-form").validate({ rules:{ login:{ required: true, minlength: 5, maxlength: 10, regexp: '[a-zA-Zа-яА-Я]{5,10}' }, }, messages: { login: { regexp: 'Username are 5-10 characters' } } }); }); 

But this method does not work. How can I make a check on the pattern? (Without PHP)

  • Oh, these zhukveri-plugs ... What does not work, then? - user207618 4:21 pm
  • regexp: '[a-zA-Zа-яА-Я] {5,10}' this expression doesn’t work - Puzzl
  • Ok, I rephrase: What mistake? What does not work or works in the form of "throw out an error"? What is the error text, if any? - user207618 pm
  • No error "throws". Just skips all non-patterned expressions. - Puzzl

1 answer 1

The problem is solved as follows:

 $(document).ready(function(){ $.validator.addMethod('username', function(value, element) { return value.match(new RegExp("^" + "[A-Za-zа-яА-я]{5,10}" + "$")); }, 'Username are 5-10 characters.') $("#registration-form").validate({ rules:{ login:{ required: true, minlength: 5, maxlength: 10, username: true }, } }); }); 
  • The secret technique of the great voodoo witch - the regular will work without a constructor: match(/^[A-Za-zа-яА-я]{5,10}$/) . And it is better to use RegExp.prototype.test here. - user207618