...

View Full Version : alphanumeric



devron
03-17-2005, 06:52 AM
Hi there
I am trying to create a textbox that will accept only numbers(0(48)-9(57)), lowercase letters (a(65)-z(90)) and uppercase letters(A(96)-Z(122))
I am trying to use the following code:


<script language>
function alphanumeric(){

if ((event.keyCode>47||event.keyCode<58)|| (event.keyCode>64||event.keyCode<91) || (event.keyCode>96||event.keyCode<123))
return false

}
</script>

When I attach this to a textbox via onkeypress="return alphanumeric()" and run the page nothing showes up in the textbox as I type
But if I leave off the 2 lines in red I can type the numbers into the textbox
any help?

Kor
03-17-2005, 09:23 AM
I guess that you should see the problem from a different angle. I think that is easier to use Regular Expresions to validate 0-9 numbers and A-z letters, than to capture the event (capture event codes are different in IE and Mozilla)

Try this


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script language="JavaScript" type="text/JavaScript">
function validate(field) {
var re = /^[0-9-A-z]*$/;
if (!re.test(field.value)) {
alert('Value must be only numbers or letters!');
field.value = field.value.replace(/[^0-9-A-z]/g,"");
}
}
</script>
</head>
<body>
<textarea name="text" cols="" rows="" onkeyup="validate(this)"></textarea>
</body>
</html>

devron
03-17-2005, 04:39 PM
Thank you

Kor
03-17-2005, 04:49 PM
You're welcome :thumbsup:
Come here any time you might have encoutered javascript problems. We might help you anytime, according to our knowledge, of course... ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum