The following JavaScript function takes 3 parameters. If the value parameter falls outside the range of the min and max values an alert dialog is displayed to that affect.
function checkvalue(value,min,max)
{
if (value<min || value>max)
alert('The number must be from ' + min + ' to '
+ max);
}
The above function can be used to check a input field on a form as shown below.
<FORM METHOD=POST>
Enter a number from 1 to 10
<INPUT TYPE=text NAME="no" VALUE="" onChange="checkvalue(this.value,1,10);">
</FORM>
Test the above in an HTML form.
You could have handled the value checking inline as shown below:
<FORM METHOD=POST>
Enter a number from 1 to 10
<INPUT TYPE=text NAME="no" VALUE=""
onChange="if (value<1
|| value>10) alert('The number must be from 1 to 10');">
</FORM>
This would work, but not be as flexible as using a function. Also if the program is long, it can be far more error prone to do it this way. It also detracts from the understandability of the form source code.