I'm trying to validate a data output from a prompt. It works fine without the data validation but with it I get "Illegal return statement".
The code is below:
while (true){
var age = prompt("How old are you?");
if(age.length === 0) {
alert("Please enter an age.");
}
else if(!age.match(/^[0-9]+$/)) {
alert("Your age must be numeric.");
}
else {
return false;
}
}
Could anyone help?
Is the while loop used in a function? I guess return statements are only valid in functions.
You should try using 'break' to get out of the loop.
what larosc said. Also if you were to google the error, the first thing that comes out is [url]http://stackoverflow.com/a/16068325[/url]
[quote]return only makes sense inside a function. There is no function in your code[/quote]
Sorry, you need to Log In to post a reply to this thread.