Accept string with letters, numbers or dots

Asked

Viewed 276 times

0

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

Ex: joao, joao22, joao.maluco22

How to validate this with jQuery Validate?

The pattern of the methods is like this:

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

1 answer

1


Add a method to 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 linked to a class, so inputs with the class validar-usuario will be validated by this new rule validarUsuario. You can add more than one rule as well, for example, make the field mandatory by also adding the rule required.

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

Your input looks like this:

<input type="text" class="validar-usuario" id="ipt_usuario">

Browser other questions tagged

You are not signed in. Login or sign up in order to post.