Method prompt JS - 'if' 'else if'

0

I am still a layman in JS, but I believe that this question is simple, so if you enter a numeric value instead of letters, it will write on the screen 'Wrong Instance!' and if he type some letter other than my name 'Elienay' he would return to 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"){

}
    
asked by anonymous 03.07.2018 / 01:56

1 answer

1

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

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

(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();
  }
})();
    
03.07.2018 / 02:12