Variable to validate 'Login' field?

Asked

Viewed 87 times

1

I’m having trouble creating a variable to validate my Login field.

For example, in my 'First' and 'Last' fields, I have this variable:

var padrao = /[^a-zà-ú]/gi;

Valid in such a way: if 'Name' or 'Surname' is not according to such variable, that is, it cannot contain numbers or special characters, only those of the variable, it gives an error message.

I want to do the same with my 'Login' (username) field, I want it to be a variable that cannot enter accents or special characters, only numbers and letters, uppercase or lowercase.

Could help me in creating such a variable?

2 answers

3


When you use ^ in square brackets ([]), you are saying that nay either the characters in the brackets - this expression is also called Negated Character Class.

But if you want an expression that accepts only letters (lowercase and uppercase) and numbers, you should remove the ^ of the brackets and put the characters you need inside them.

Also, it would be interesting to put a ^ at the beginning (outside the brackets at the beginning, means "beginning of string"), and a $ at the end (meaning "end of string").

I also put a quantifier + meaning "one or more occurrences". The final expression is like this:

var padrao = /^[a-zA-Z0-9]+$/;

console.log(padrao.test('abcABC01')); // true
console.log(padrao.test('ábc')); // tem caractere acentuado, retorna false

  • ^ is the "beginning of the string""
  • [a-zA-Z0-9]+ is "one or more occurrences of lower case letters (a-z), capital letters (A-Z) or numbers (0-9)"
  • $ is the "end of the string"

That is, it checks whether all characters in the string are letters or numbers.

1

  • The circumflex ^ marks the beginning of a line
  • the dollar sign $ marks the end of a line
  • i whatever, upper or lower case
  • [a-z] letters in the range of a until z, when we have a dash - between two characters, this represents the entire interval between them
  • [] set, any character included in the set, including space

var validacao = /^[a-záàâãéèêíïóôõöúçñ]+$/i
var nomes = ["Fulano", "Fulano com espaços", "Céçlañô", "ze234", "FulanoSemEspaçosComcecedilha", "FulanoSemEspacosSemcecedilha"]
nomes.forEach(function(nome) {
    console.log(nome + ": " + validacao.test(nome))
});

within the set you can set the allowed letters :)

Finally

in your case, only numbers and letters, uppercase or lowercase

var validacao = /^[a-z0-9]+$/i
var nomes = ["Fulano", "Fulano com espaços", "Céçlañô", "ze234", "FulanoSemEspaçosComcecedilha", "FulanoSemEspacosSemcecedilha"]
nomes.forEach(function(nome) {
    console.log(nome + ": " + validacao.test(nome))
});

as said before, the modifier i is used for case insensitive matching.

Another way:

var validacao = /^[a-z\d]+$/i
var nomes = ["Fulano", "Fulano com espaços", "Céçlañô", "ze234", "FulanoSemEspaçosComcecedilha", "FulanoSemEspacosSemcecedilha"]
nomes.forEach(function(nome) {
    console.log(nome + ": " + validacao.test(nome))
});

\d the same as [0-9]

We can further reduce the expression

var validacao = /^[\w]+$/
var nomes = ["Fulano", "Fulano com espaços", "Céçlañô", "ze234", "FulanoSemEspaçosComcecedilha", "FulanoSemEspacosSemcecedilha", "tudominusculas", "TUDOMAIUSCULAS"]
nomes.forEach(function(nome) {
    console.log(nome + ": " + validacao.test(nome))
});

\w - alfanuerico

Browser other questions tagged

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