Tomcat
October 4th, 2006, 09:06 AM
Hi,
I'm using OnChange (JavaScript) to validate user input. If the entered value is incorrect I pop up an alert, restore the default value and return false to give the user a chance to correct the value without submitting the form.
This works fine in IE and Opera, but if I use Firefox (1.5.0.7) it only works if I leave the input field with tab or by clicking outside the control. If I click on Submit the alert is displayed, but at the same time the form is submitted!
Sample code:
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
function Validate(ctrl)
{
if("NaN" == "" + Number(ctrl.value) ||
Number(ctrl.value) < 0 ||
Number(ctrl.value) > 100)
{
alert("A number between 0 and 100 is required.");
ctrl.value = ctrl.defaultValue;
return false;
}
return true
}
</SCRIPT>
</HEAD>
<BODY>
<FORM ACTION="result.cgi" METHOD="POST" TARGET="_self">
Enter a number (0-100):
<INPUT TYPE="TEXT" SIZE=10 MAXLENGTH=10 VALUE=0
NAME="test" ONCHANGE="return Validate(this);">
<BR>
<INPUT TYPE="SUBMIT" VALUE="Sumbit">
</FORM>
</BODY>
</HTML>
I'm really not a web developer, so I have no idea if there are better ways to do this. What I do know is that I don't want to reload the page since this is an embedded application with limited resources (CPU/bandwidth), but fire away any ideas you might have.
I'm using OnChange (JavaScript) to validate user input. If the entered value is incorrect I pop up an alert, restore the default value and return false to give the user a chance to correct the value without submitting the form.
This works fine in IE and Opera, but if I use Firefox (1.5.0.7) it only works if I leave the input field with tab or by clicking outside the control. If I click on Submit the alert is displayed, but at the same time the form is submitted!
Sample code:
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
function Validate(ctrl)
{
if("NaN" == "" + Number(ctrl.value) ||
Number(ctrl.value) < 0 ||
Number(ctrl.value) > 100)
{
alert("A number between 0 and 100 is required.");
ctrl.value = ctrl.defaultValue;
return false;
}
return true
}
</SCRIPT>
</HEAD>
<BODY>
<FORM ACTION="result.cgi" METHOD="POST" TARGET="_self">
Enter a number (0-100):
<INPUT TYPE="TEXT" SIZE=10 MAXLENGTH=10 VALUE=0
NAME="test" ONCHANGE="return Validate(this);">
<BR>
<INPUT TYPE="SUBMIT" VALUE="Sumbit">
</FORM>
</BODY>
</HTML>
I'm really not a web developer, so I have no idea if there are better ways to do this. What I do know is that I don't want to reload the page since this is an embedded application with limited resources (CPU/bandwidth), but fire away any ideas you might have.