function nameValidation(){
var x = eval(prompt("please enter a value"));
var i = 0;
while(i < Infinity){
if(x !== "abcdefghi"){
alert("please enter a valid name")
x = eval(prompt("please enter a value"));
}
else if (x === "abcdefghi"){
break
}
i++
}
}
nameValidation()
I am trying to validate a name using eval() and prompt(). When I write any string I get an error:
VM146:1 Uncaught Reference Error: klklk is not defined at eval (eval at nameValidation (Java-Script:7), :1:1)at nameValidation (:7:11)at :15:1.
I tried to use other conversion methods but I got an infinite loop without stop, although I am using break in my code.
function nameValidation(){
var x = eval(prompt("please enter a value"));
var i = 0;
while(i < Infinity){
if(x !== "abcdefghi"){
alert("please enter a valid name")
x = eval(prompt("please enter a value"));
}
else if (x === "abcdefghi"){
break
}
i++
}
}
I expected the program to work without errors, on the other hand, I expect to have the alert: please enter a valid name if the name is not a string and if the name is a string the programme breaks out of the loop.
In that code the eval() evaluates the x, if it is not a string the program will alert "please enter a valid name". That's because I use identically, not equal operator ("!==") to check the type and if x identically equal to string the program breaks out of the loop. My concern is why I receive the error above in my question.