Method prompt JS - 'if' 'Else if'

Asked

Viewed 73 times

0

I am still a layman in JS, but this question I believe to be simple, I intend if if the user type a numerical value instead of letters, he write on the screen 'Wrong instance! ' and if he typed any letter different from my name 'Elienay' he returned to the question, repeat it again

var name = prompt("Digite seu nome:","");
if (name=="Elienay") {
document.write("Exato");
}
else if(name== 0 to 999){
	document.write("Instancia errada!");
}
else if(name != "Elienay"){

}

1 answer

1


You can use the function match to validate the input by passing a regex.

To repeat whenever the input is not valid encapsulate the code in a function call it

(function ask() {
  var name = prompt("Digite seu nome:", "");

  if (name.match(/Elienay/i)) {
    document.write("Exato");
  } else if (name.match(/[0-9]+/)) {
    document.write("Instancia errada!");
  } else if (name != "Elienay") {
    ask();
  }
})();

  • If it is a letter or a sequence different from my name, it has to make the return, repeat the question, what worked in your code was if you type a number

  • at the end, it is not for him to write anything, because if you type some letter out of my name and some letter out of sequence, he has to repeat the question, understood..

  • and from what it seems he is considering wrong if I type my name with the first minuscule letter

  • I edited the question by adding

Browser other questions tagged

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