Variable to validate 'Login' field?

1

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

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

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

In this way, if 'Name' or 'Last name' does not conform to that variable, that is, it can not contain numbers or special characters, only those of the variable, it gives an error message.

I want to do the same with my 'Login' field, I want it to be a variable that can not enter accents or special characters, just numbers and letters, uppercase or lowercase.

Could you help me in creating such a variable?

    
asked by anonymous 20.06.2018 / 03:27

2 answers

3

When you use ^ inside brackets ( [] ), you're saying you do not want the characters that are 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% with% of the brackets and put the characters you need inside them.

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

I also put a% quantifier with% meaning "one or more occurrences". The final expression looks 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 "start of string" "
  • + is "one or more occurrences of lowercase letters ( ^ ), uppercase ( [a-zA-Z0-9]+ ) or numbers ( a-z )"
  • A-Z is the "end of string"

That is, it checks to see if all the characters in the string are letters or numbers.

    
20.06.2018 / 03:36
1
  • The% circumflex ^ marks the beginning of a line
  • The dollar sign $ marks the end of a line
  • i is case sensitive,
  • [a-z] letters in the range of a to z , when we have a dash - between two characters, this represents the entire range 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

For your case, just 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 mentioned earlier, the i modifier is used to perform 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 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

    
20.06.2018 / 06:01