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 4 of 4

Thread: alphanumeric

  1. #1
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    alphanumeric

    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?

  • #2
    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
    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

    PHP 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 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> 
    Last edited by Kor; 03-17-2005 at 04:51 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    alphanumeric

    Thank you

  • #4
    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
    You're welcome
    Come here any time you might have encoutered javascript problems. We might help you anytime, according to our knowledge, of course...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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