Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

304
Visualizações
Validating name in visual studio code
var userName = input.question('Please enter your name: '); //Asking User to enter their Name.
while (userName.includes('.')) {
    console.log ("Invalid Name!");
    var userName = input.question('Please enter your name: '); //Asking User to enter their Name.
}

Above code will ask the user his/her name and store it in "userName". Then it will validate using .includes to check unwanted characters and numbers.

I want to validate if userName has numbers or unwanted characters such as "?/.,;'[]{}|&^%@" etc. I have tried using .includes and validate if a name has "." However, I'm not sure how to go about from there to validate the rest.

After the while checks that it contains the unwanted characters, it will re-prompt the user to enter a new name and it will check again until it returns false.

Is there a solution to this?

about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

You can use REGEX to search for non-alphabetic or space characters in the string:

userName.search(/^[a-zA-Z\s]+$/)

The response will be 0 or -1. 0 means that no characters except A-Z, a-z and space were found, -1 means the contrary.

Edit: I found similar question with more detailed answers here.

about 4 years ago · Santiago Gelvez Relatório

0

let chars = /[$&+,:;=?@#|'<>.^*()%!-]/g;
chars.test(UserName)

Use Regular expressions ( Regex ) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda