Monday, 15 April 2013

html - JavaScript form validation: Why does the first work but the second doesn't? -



html - JavaScript form validation: Why does the first work but the second doesn't? -

i have next form:

<form action="next.html" id="userinput" method="post" onsubmit="return validate();"> age: <input type="text" name="age" id="age"/> function validate() { var age = document.getelementbyid("age").value; if(age > 100 || age < 0) { alert("age must within 0 , 100"); homecoming false; } else { homecoming true; } }

this works normal. if come in number in age textbox great 100, show error message , remain @ current form page.

however, if utilize variable errormessage next show alert box, doesn't work. go next page without poping alert error message.

function validate() { var age = document.getelementbyid("age").value; var errormessage=""; if(age > 100 || age < 0) { errormessage = "age must within 0 , 100"; } if( errormessage !== "" ) alert(errormessage); homecoming false; } else { homecoming true; } }

so why first work sec doesn't?

i see missing left brace after condition. code should be:

if( errormessage !== "" ){ alert(errormessage); homecoming false; } else { homecoming true; }

javascript html forms html-form

No comments:

Post a Comment