I am trying to make a script in which a prompt window will pop up asking a question, and based on what the answer in it is, an alert box will pop up saying that the answer is valid or invalid. In my code, my prompt box works, but my alert box didn't. Can someone help me solve this problem please? Thank you so so much!!
<!DOCTYPE HTML>
<html>
<script type="text/javascript">
var City=prompt("Enter your city", "City");
function checkName(){
var validLetters=/^[a-z]+$/i;
if(validLetters.test(City))
alert("Your input is accepted!");
else
alert("Your input is invalid!");
}
</script>
Invoke the function checkName
with argument City
or you could use global-variable
.
You are not calling the function in your script to test the value of prompt
var City = prompt("Enter your city", "City");
checkName(City);
function checkName(City) {
var validLetters = /^[a-z]+$/i;
if (validLetters.test(City))
alert("Your input is accepted!");
else
alert("Your input is invalid!");
}