0

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;
 }

 }
1
  • The call "return checkUserInput()" does not pass any params to the called function, meaning "whatevertheyentered" will be overridden to null. Commented Sep 2, 2011 at 20:46

1 Answer 1

2

You could access it using document.form[0].user.value (assuming it's the fist form on the page).

Or give the "user" input box an ID and find it using document.getElementById("theId").

Do both within your checkUserInput function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.