Accept string with letters, numbers, or periods

0

I'm using the Jquery Validate plugin to validate facebook usernames, and by default it can have letters, numbers, and periods.

Ex: joao, joao22, joao.maluco22

How to validate this with jQuery Validate?

The default methods are as follows:

jQuery.validator.addMethod("lettersonly", function(value, element) {
    return this.optional(element) || /^[a-z]+$/i.test(value);
}, "Somente letras");
    
asked by anonymous 29.11.2016 / 02:25

1 answer

1

Adds a method to the validator

$.validator.addMethod("validarUsuario", function(value, element) {
  return this.optional( element ) || /^[a-zA-Z0-9.]+$/.test( value );
}, 'Informe um usuário válido.');

Adds a rule bound to a class, so inputs with class validar-usuario will be validated by this new rule validarUsuario . You can add more than one rule too, for example, make the field mandatory, also adding the required rule.

$.validator.addClassRules("validar-usuario", {
    "validarUsuario": true
});

Your input looks like this:

<input type="text" class="validar-usuario" id="ipt_usuario">
    
29.11.2016 / 03:42