I'm trying to create my own anti-spam filter by generating random numbers that the user has to enter into a text input form field, if it's correct they proceed to the next page, if not it displays an error that the number entered is incorrect.
I have the following code for my html form:
<form name="input" action="nextpage.html" onSubmit="return checkUserInput()" method="get">
Number:
<input type="text" name="user" />
<input type="submit" value="Submit" />
</form>
The checkUserInput() is a javascript function which will take the user's input, compare it to the random number that was displayed to make sure they're the same, then return true, or false if the two numbers are not the same.
How do I get the input that the user entered into the text field form and pass it to the checkUserInput() function. I was thinking it would be "return checkUserInput(INPUT)" but I'm not sure how to properly pass the user's input to the function.
A secondary issue I have is I'd like the page to refresh with a different random number and an error message to be displayed if the user clicks "Submit" when the numbers aren't the same. Right now the only action that exists is: action="nextpage.html" but I want an option for when checkUserInput() returns false. Is there a way to have two different actions based on whether checkUserInput() returns true or false?
Thanks, Joe
Here's also my checkUserInput() function:
var whatevertheyentered;
function checkUserInput(whatevertheyentered){
if(whatevertheyentered == randomnumber){
return true;
}
else {
return false;
}
}