Home > Abakada: Back to Basics > Basic Scripting > Data Entry Validation > Checking for a valid range of values
Abakada ~ Back to Basics
This example shows a sample validation routine that checks for a given range of numbers. The range to test is variable, and in this sample is from 18
to 45
. If the input is within these values, the function accepts; else, it rejects.
Now, let's explore the code.
<script language="JavaScript">
<!--
function validateRange ( fld, lowval, hival ) {
if ( fld.value=='' ) {
alert ( 'Please enter a value' )
fld.select ( ); return false
}
else if ( ( fld.value < lowval ) || ( fld.value > hival ) ) {
var msg = ''
if ( fld.value < lowval ) { msg = 'LESS' }
else { msg = 'MORE' }
alert ( 'Sorry, the entry is ' + msg + ' than expected.' )
fld.select ( ); fld.focus ( ); return false
}
else alert ( 'Yes! Welcome to the Club.' )
return true
}
//-->
</script>
<form onsubmit="return validateRange ( age, 18, 45 )">
<input type="text" name="age" size="3" maxlength="3">
<input type="submit" value="Check">
</form>
Validating Against a Range of Values ( Web Forms ) Web Forms Validation