Web Hosting Directory | Web Hosting Resources | Webmaster Resources | Domain Tools | Search Engine Marketing | Forums
  Signup
Today: 395   Yesterday: 1,023   Max: 1,872   Total: 419,071   Current Users : 10 (Member 0) 
DOMAIN TOOLS

WEBMASTER TOOLS

RELATED ARTICLES

SUBMIT AN ARTICLE

Write for us and get publicity, link to your website, and best of all get paid. Please visit our writers page for details.


LINK TO US

Link to us and earn points for each visitor you bring to us. Please visit our link partner page for details.

 

Restricting a Field to Alphanumeric Only

JavaScript 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 -- no spaces, commas or other types of characters are allowed. This technique is useful if you are asking for information such as a User ID which has only alphanumeric characters.

First, copy this JavaScript code into the <HEAD> section of your document:

<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 onKeyPress attribute. Use the exact onKeyPress attribute highlighted in this example. It is important that you use return in the attribute or the technique won't work..

<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:

User ID:

The next page shows a similar technique which allows you to restrict a field to just numbers.



Copyright 1997-2002 Idocs, Inc. Written by Miko Sullivan.

 
 
What is your major source of website traffic?
 
 
 
 
 
 

ADVERTISEMENT

 
Cheap Web HostingBudget Web HostingEcommerce Web Hosting
Link to UsLink ExchangeAdvertisePrivacy PolicyTerms Of ServiceAbout UsContact UsSitemap
Copyright © 2006 - 2008 Broadband Media, Inc. All rights reserved.