简体   繁体   中英

How to detect a invalid prompt answer?

Example of what I am trying to achieve:

 var p = prompt("What is your favorite number?", 1, 2, 3, 4); if (p) { function checkForInvalid() { const invalid = "invalid" } } if (p === invalid) { alert("Sorry, I don't know that number"); }

or something similar. I am relatively new to javascript. Could I use a function to check if the number is not specified within the prompt? I am not trying to validate anything...

 const validNumbers = [1,2,3,4]; var p = prompt("What is your favorite number?", validNumbers[0]); if (validNumbers.indexOf(Number(p)) == -1) { alert("Sorry, I don't know that number"); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM