开发者

How to get value user entered into html text form field passed to javascript function?

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 prop开发者_C百科erly 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;
 }

 }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜