asp.net.ph

Skip Navigation LinksHome > Abakada: Back to Basics > Basic Scripting > Data Entry Validation > Checking for a valid range of values

Data Entry Validation

Abakada ~ Back to Basics


Checking for a valid range of values

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.

Please enter your age

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> 
See Also

Validating Against a Range of Values ( Web Forms )   Web Forms Validation


Back to top


© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note