Is There an example on how to acces numbers only in Text Field?

A

Anonieko Ramos

You can use the OnKeypress javascript event
Evaluate the keypress on the function
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>(Wiki)
browse My Wiki Home

</title>
<LINK REL=StyleSheet HREF="wiki.css" TYPE="text/css" >
<SCRIPT TYPE="text/javascript">
<!--
// copyright 1999 Idocs, Inc. http://www.idocs.com
// Distribute this script freely but keep this notice in place
function numbersonly(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
key = window.event.keyCode;
else if (e)
key = e.which;
else
return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) ||
(key==9) || (key==13) || (key==27) )
return true;

// numbers
else if ((("0123456789").indexOf(keychar) > -1))
return true;

// decimal point jump
else if (dec && (keychar == "."))
{
myfield.form.elements[dec].focus();
return false;
}
else
return false;
}

//-->
</SCRIPT>


</head>
<body>


<FORM ACTION="/cgi-bin/mycgi.pl" METHOD=POST>
U.S. ZIP Code:
<INPUT NAME="dollar" SIZE=5 MAXLENGTH=5
onKeyPress="return numbersonly(this, event)">
<INPUT TYPE=SUBMIT VALUE="go">
</FORM>

</body>
</html>
<noscript>
<script language="jawascript">
 
M

Michael Winter

Subject: Is There an example on how to acces numbers only in Text Field?
You can use the OnKeypress javascript event
Evaluate the keypress on the function

Are you trying to ask a question, or make a statement? It seems the
latter. In either case, it would be nice to actually present valid markup.

The best approach, in my opinion, is to not even try to prevent
keystrokes. This can lead to confusion or irritation if the user doesn't
realise that a keystroke has been ignored, and before it's suggested,
alerting the user to it isn't a good solution. That'll lead to more
frustration.

Instead, validate the field after the user has finished entering their
value. It's far more kind to the user and more reliable.

[snipped malformed rubbish]

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top