Go Back   CodingForums.com > :: Client side development > JavaScript programming > Post a JavaScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-24-2002, 01:38 PM   PM User | #1
whammy
Senior Coder

 
Join Date: Jun 2002
Location: 41° 8' 52" N -95° 53' 31" W
Posts: 3,660
Thanks: 0
Thanked 0 Times in 0 Posts
whammy is an unknown quantity at this point
Accept only letters in a field

Very simple, but useful - I just came up with this in answer to a post:
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>Letters ONLY</title>
<script type="text/javascript">
<!--
function alpha(e) {
     var k;
     document.all ? k = e.keyCode : k = e.which;
     return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8);
}
// -->
</script>
</head>
<body>
     <div>
          <form id="example" action="javascript://">
               <input type="text" onkeypress="return alpha(event)" />
          </form>
     </div>
</body>
</html>
Post any improvements if you have 'em.
__________________
Former ASP Forum Moderator - I'm back!

If you can teach yourself how to learn, you can learn anything. ;)

Last edited by whammy; 12-07-2002 at 10:46 PM..
whammy is offline   Reply With Quote
Old 11-24-2002, 04:20 PM   PM User | #2
Borgtex
Regular Coder

 
Join Date: Aug 2002
Location: Spain
Posts: 420
Thanks: 0
Thanked 0 Times in 0 Posts
Borgtex is an unknown quantity at this point
Re: Accept only letters in a field

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

Last edited by Borgtex; 11-24-2002 at 04:24 PM..
Borgtex is offline   Reply With Quote
Old 11-26-2002, 12:21 AM   PM User | #3
whammy
Senior Coder

 
Join Date: Jun 2002
Location: 41° 8' 52" N -95° 53' 31" W
Posts: 3,660
Thanks: 0
Thanked 0 Times in 0 Posts
whammy is an unknown quantity at this point
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. ;)
whammy is offline   Reply With Quote
Old 12-06-2002, 02:32 PM   PM User | #4
skeatt
New Coder

 
Join Date: Sep 2002
Location: Australia
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
skeatt is an unknown quantity at this point
Question


Whats this doing here...

return (allow.indexOf(String.fromCharCode(k))!=-1);


and whats this mean...

<form id="example" action="java script:// ">
skeatt is offline   Reply With Quote
Old 12-06-2002, 03:38 PM   PM User | #5
mordred
Senior Coder


 
Join Date: Jun 2002
Location: frankfurt, german banana republic
Posts: 1,848
Thanks: 0
Thanked 0 Times in 0 Posts
mordred is an unknown quantity at this point
allow

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.
mordred is offline   Reply With Quote
Old 12-07-2002, 11:17 PM   PM User | #6
whammy
Senior Coder

 
Join Date: Jun 2002
Location: 41° 8' 52" N -95° 53' 31" W
Posts: 3,660
Thanks: 0
Thanked 0 Times in 0 Posts
whammy is an unknown quantity at this point
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. ;)

Last edited by whammy; 12-14-2002 at 12:03 PM..
whammy is offline   Reply With Quote
Old 12-08-2002, 09:23 AM   PM User | #7
skeatt
New Coder

 
Join Date: Sep 2002
Location: Australia
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
skeatt is an unknown quantity at this point
Quote:
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.

Last edited by skeatt; 12-08-2002 at 09:29 AM..
skeatt is offline   Reply With Quote
Old 02-28-2008, 09:30 AM   PM User | #8
olaster
New to the CF scene

 
Join Date: Feb 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
olaster is an unknown quantity at this point
enter key

The solutions here allow using certain letters but the enter key doesn't submit the form. Anyone knows how to allow also the enter key?
olaster is offline   Reply With Quote
Old 02-28-2008, 09:44 AM   PM User | #9
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
Here's a short variant, RegExp based only:
Code:
<!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 type="text/JavaScript">
function valid(f) {
!/^[A-z]*$/.test(f.value)?f.value = f.value.replace(/[^A-z]/g,''):null;
} 
</script>
</head>
<body><br>
<form id="myform" action="">
<input name="mytext" type="text" onkeyup="valid(this)" onblur="valid(this)">
</form>
</body>
</html>
olaster, this code will avoid the Enter key problem as well.
whammy, this code will avoid the backspace problem.
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Last edited by Kor; 02-28-2008 at 09:46 AM..
Kor is offline   Reply With Quote
Old 02-28-2008, 09:57 AM   PM User | #10
olaster
New to the CF scene

 
Join Date: Feb 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
olaster is an unknown quantity at this point
Thanks a lot Kor
olaster is offline   Reply With Quote
Old 02-28-2008, 10:13 AM   PM User | #11
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
As for the special "non-english" letters, the code is to be adapted upon needs:
Code:
<script type="text/JavaScript">
function valid(f) {
!/^[A-zÇÑQÀÁÈÉÍÌÏÓÒÚÙÜ]*$/i.test(f.value)?f.value = f.value.replace(/[^A-zÇÑQÀÁÈÉÍÌÏÓÒÚÙÜ]/ig,''):null;
} 
</script>
And this, whammy, will solve your second problem, I guess
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 03-16-2008, 07:05 PM   PM User | #12
YaymeQ
Regular Coder

 
YaymeQ's Avatar
 
Join Date: Feb 2008
Location: virginia
Posts: 140
Thanks: 14
Thanked 0 Times in 0 Posts
YaymeQ is an unknown quantity at this point
so what if people disable javascript and then type whatever they want?
__________________
Where is the "any" key??!
YaymeQ is offline   Reply With Quote
Old 03-16-2008, 08:04 PM   PM User | #13
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
Quote:
Originally Posted by YaymeQ View Post
so what if people disable javascript and then type whatever they want?
a server side protection validation is to be used as well, and that will be enough.
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 03-31-2008, 10:27 AM   PM User | #14
shibu
New Coder

 
Join Date: Mar 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
shibu is an unknown quantity at this point
<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>

Thanks and regards
http://www.outsource-website-development.com/ http://www.outsource-website-design.com/
shibu is offline   Reply With Quote
Old 04-06-2008, 09:43 PM   PM User | #15
discobear
New to the CF scene

 
Join Date: Apr 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
discobear is an unknown quantity at this point
this is really cool but the original version is much more practical as the revised version prevents the user from deleting characters.
discobear is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:50 AM.


Advertisement
Log in to turn off these ads.