Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!