| |||||||||||
Data Entry ValidationChecking for alphabet entries onlyThe following is a sample validation routine that submits the form only when a text field does not contain digits or characters other than lower- or upper-case letters. Now, let's see how that is done. Basically, there are two handlers that test the entry:
<script language="JavaScript">
<!--
function isAlpha ( evt ) {
var keyCode = evt.which ? evt.which : evt.keyCode;
alpha = ( keyCode >= 'a'.charCodeAt ( ) &&
keyCode <= 'z'.charCodeAt ( ) ) ||
( keyCode >= 'A'.charCodeAt ( ) &&
keyCode <= 'Z'.charCodeAt ( ) ) ||
( keyCode >= 8 && keyCode <= 46 );
return ( alpha );
}
function validateAlpha ( text ) {
for ( c=0; c < text.length; c ++ ) {
alpha = ( text.charCodeAt ( c ) >= 65 &&
text.charCodeAt ( c ) <=90 ) ||
( text.charCodeAt ( c ) >= 97 &&
text.charCodeAt ( c ) <=122 )
if ( !alpha ) {
alert ( 'No way, sorry.' );
document.theForm.theField.select ( );
return false;
}
}
return true;
}
//-->
</script>
<form name="theForm"
onsubmit="return validateNumber ( this.theField.value )">
<input name="theField" onkeydown="
if ( !isAlpha ( event ) ) {
alert ( 'Oops! letters only please.' );
return false;
}">
</form>
As the user types in a key, the function The handler returns Note however, that characters entered thru the numeric keypad ( with NumLock on ) are not trapped at the field level. The keyCode values between When the form is submitted, the function This ensures that in cases wherein the text input accepts an invalid entry that is not trapped at the field-level, such as characters entered from the numeric keypad with NumLock on, the form-level handler will still invalidate the submission. Note, however, that the behavior of this validation routine has not been identified in cases of operating system and browser combinations that do not conform to the Unicode standard and may return non-standard keyCode values. See AlsoValidating Against a Data Type ( Web Forms ) Web Forms Validation |
| ||||||||||
Check out related books at Amazon
© 2000-2008 Rey Nuñez All rights reserved.
If you have any question, comment or suggestion
about this site, please send us a note
You can help support aspxtreme