Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

157
Vistas
Setting a character limit on a prompt box Javascript

I would like to know if there is a way to pre-set the length of the characters in the javascript prompt. So that I can enter only one character and no more

var carattere = prompt("inserte a character");


console.log(carattere);

if (carattere=="a" || carattere=="e" || carattere=="i" || carattere=="o" || carattere=="u") {
    window.alert("the character is a Vowel");
}else if(carattere=="y"){
    window.alert("Y can be consonant or vowel");
}else{
    window.alert("the character is a consonant");
}

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Since prompt() stores a string by default, You could check for the length of the prompt using length. Like this:

const carattere = prompt("Insert a character");


if (carattere.length === 1) {
  if (carattere === "a" || carattere === "e" || carattere === "i" || carattere === "o" || carattere === "u") {
    window.alert("The character is a Vowel");
  } else if (carattere === "y") {
    window.alert("Y can be consonant or vowel");
  } else {
    window.alert("The character is a consonant");
  }
} else {
  console.log("Please limit the input to a single character");
}

Note: Typing a number character gets identified as consonant. You might want to add an additional check (like a regular expression) to account for that.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda