Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    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

    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.
    Last edited by whammy; 12-07-2002 at 10:46 PM.
    Former ASP Forum Moderator - I'm back!

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

  • #2
    Regular Coder
    Join Date
    Aug 2002
    Location
    Spain
    Posts
    420
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Accept only letters in a field

    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=' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
    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>
    Last edited by Borgtex; 11-24-2002 at 04:24 PM.
    Don't resist to assimilation. Billions of Borgs can't be wrong!

  • #3
    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
    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. ;)

  • #4
    New Coder
    Join Date
    Sep 2002
    Location
    Australia
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question


    Whats this doing here...

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


    and whats this mean...

    <form id="example" action="java script:// ">

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    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
    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.

    Last edited by whammy; 12-14-2002 at 12:03 PM.
    Former ASP Forum Moderator - I'm back!

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

  • #7
    New Coder
    Join Date
    Sep 2002
    Location
    Australia
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #8
    New to the CF scene
    Join Date
    Feb 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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?

  • #9
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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.
    Last edited by Kor; 02-28-2008 at 09:46 AM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #10
    New to the CF scene
    Join Date
    Feb 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks a lot Kor

  • #11
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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&#199;&#209;Q&#192;&#193;&#200;&#201;&#205;&#204;&#207;&#211;&#210;&#218;&#217;&#220;]*$/i.test(f.value)?f.value = f.value.replace(/[^A-z&#199;&#209;Q&#192;&#193;&#200;&#201;&#205;&#204;&#207;&#211;&#210;&#218;&#217;&#220;]/ig,''):null;
    } 
    </script>
    And this, whammy, will solve your second problem, I guess
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #12
    Regular Coder YaymeQ's Avatar
    Join Date
    Feb 2008
    Location
    virginia
    Posts
    140
    Thanks
    14
    Thanked 0 Times in 0 Posts
    so what if people disable javascript and then type whatever they want?
    Where is the "any" key??!

  • #13
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #14
    New Coder
    Join Date
    Mar 2008
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <head>
    <script type="text/javascript">
    <!--
    var letters=' ABC&#199;DEFGHIJKLMN&#209;OPQRSTUVWXYZabc&#231;defghijklmn&#241;opqrstuvwxyz&#224;&#225;&#192;&#193; &#233;&#232;&#200;&#201;&#237;&#236;&#205;&#204;&#239;&#207;&#243;&#242;&#211;&#210;&#250;&#249;&#21 8;&#217;&#252;&#220;'
    var numbers='1234567890'
    var signs=',.:;@-\''
    var mathsigns='+-=()*/'
    var custom='<>#$&#37;&?&#191;'

    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/

  • #15
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    this is really cool but the original version is much more practical as the revised version prevents the user from deleting characters.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •