Restricting a Field to Alphanumeric OnlyJavaScript can be used to guide the user in entering correct data. This page describes a method for restricting entered data to just letters and numbers
First, copy this JavaScript code into the
<SCRIPT TYPE="text/javascript">
<!--
// copyright 1999 Idocs, Inc. http://www.idocs.com
// Distribute this script freely but keep this notice in place
function letternumber(e)
{
var key;
var keychar;
if (window.event)
key = window.event.keyCode;
else if (e)
key = e.which;
else
return true;
keychar = String.fromCharCode(key);
keychar = keychar.toLowerCase();
// control keys
if ((key==null) || (key==0) || (key==8) ||
(key==9) || (key==13) || (key==27) )
return true;
// alphas and numbers
else if ((("abcdefghijklmnopqrstuvwxyz0123456789").indexOf(keychar) > -1))
return true;
else
return false;
}
//-->
</SCRIPT>
Using this script we can set a field as alphanumeric using the
<FORM ACTION="../cgi-bin/mycgi.pl" METHOD=POST> User ID: <INPUT NAME=ID MAXLENGTH=8 onKeyPress="return letternumber(event)"> <INPUT TYPE=SUBMIT VALUE="go"> </FORM> This creates this form: The next page shows a similar technique which allows you to restrict a field to just numbers. |