Originally posted by whammy Post any improvements if you have 'em.
Ok this improved version works with character patterns already defined or defined by user. That's useful for different languages support, mail, urls, etc...
Code:
<head>
<script type="text/javascript">
<!--
var letters=' ABCÇDEFGHIJKLMNÑOPQRSTUVWXYZabcçdefghijklmnñopqrstuvwxyzàáÀÁéèÈÉíìÍÌïÏóòÓÒúùÚÙüÜ'
var numbers='1234567890'
var signs=',.:;@-\''
var mathsigns='+-=()*/'
var custom='<>#$%&?¿'
function alpha(e,allow) {
var k;
k=document.all?parseInt(e.keyCode): parseInt(e.which);
return (allow.indexOf(String.fromCharCode(k))!=-1);
}
// -->
</script>
</head>
<body>
<div>
<form id="example" action="java script://">
<input type="text" onkeypress="return alpha(event,numbers)" />
<input type="text" onkeypress="return alpha(event,letters)" />
<input type="text" onkeypress="return alpha(event,numbers+letters+signs)" />
</form>
</div>
</body>
</html>
__________________
Don't resist to assimilation. Billions of Borgs can't be wrong!
I like it. Still elegant and simple with minimal changes, and modifiable by what characters the programmer wants to allow... your improvements have been assimilated.
__________________
Former ASP Forum Moderator - I'm back!
If you can teach yourself how to learn, you can learn anything. ;)
is a parameter passed to the function alpha. It can consist of concatenated strings in this example (look at the eventhandlers in the input tags). The effect should be that you can restrict the letters that may be typed into the fields by passing the appropriate variables, which in return consist of those characters allowed to be typed.
Sticking to the original script, here's an improvement that defeats copying and pasting anything but letters in.
Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
<title>isAlphaKey</title>
<script type="text/javascript">
<!--
function isAlphaKey(e) {
var k;
document.all ? k = e.keyCode : k = e.which;
return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8);
}
function extractAlpha(str) {
return str.replace(/[^a-z]/gi,"");
}
// -->
</script>
</head>
<body>
<div>
<form id="example" action="javascript://">
<input type="text" onkeypress="return isAlphaKey(event)" onblur="this.value=extractAlpha(this.value)" /> Letters Only<br />
</form>
</div>
</body>
</html>
P.S. The other idea posted works fine, except I can't get regular expressions to accept any of the "non-english" characters he typed in (therefore allowing anything to be pasted in the field), and the backspace key was left out of it as well... besides you can update the keycodes to match numbers and symbols.
__________________
Former ASP Forum Moderator - I'm back!
If you can teach yourself how to learn, you can learn anything. ;)
Originally posted by whammy (therefore allowing anything to be pasted in the field),
The other posted improvement by Borgtex.....I did notice that when you clicked on a field it brought up some browser history of pass entries, clicking on one of these that had illegal characters for that field didn't seem to matter (ie numbers), it filled the field with numbers when it shouldn't have,
I'll have to look at your improvement closely and see if it can be included in all fields.
<head>
<script type="text/javascript">
<!--
var letters=' ABCÇDEFGHIJKLMNÑOPQRSTUVWXYZabcçdefghijklmnñopqrstuvwxyzàáÀÁéèÈÉíìÍÌïÏóòÓÒúùÚÙüÜ'
var numbers='1234567890'
var signs=',.:;@-\''
var mathsigns='+-=()*/'
var custom='<>#$%&?¿'
function alpha(e,allow) {
var k;
k=document.all?parseInt(e.keyCode): parseInt(e.which);
return (allow.indexOf(String.fromCharCode(k))!=-1);
}