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 2 of 2
  1. #1
    New Coder
    Join Date
    Dec 2003
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    onKeyPress function

    Hey, I have a function, like so:

    function validate()
    {
    name = document.FrontPage_Form1.Username.value;
    name_re = new RegExp("^[a-zA-Z '.-]+$", "g");

    if(name.match(name_re))
    {

    }else{
    alert('This is not a valid character for a name');
    }
    }

    to validate a pressed key to when it is entered into a text area of a form. It makes sure that values are a-z (both upper and lower case) and also a few other characters ('. and - as well as a space) I am calling this function, however, with the onKeyPress event and I wish to know how to refer to this key that is pressed, so that I can ckeck it individually to make sure it is valid. How do I do this? onKeyPress="validate(key)" how??
    Spiral Out

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Why would you need to know which key was pressed?
    Unless I am misunderstanding your application, you should
    only be concerned that the right key was pressed.

    Try this:
    Code:
    <script type="text/javascript">
    function validate(){
    name = document.form1.Username.value;
    name_re = new RegExp("^[a-zA-Z '.-]+$", "g");
    if(name.match(name_re)){
    return true;
    }
    else{
    alert('This is not a valid character for a name');
    name = name.substring(0,name.length-1);
    document.form1.Username.value = name;
    }
    }
    </script>
    </HEAD>
    
    <BODY>
    <form name="form1">
    <input type="text" name="Username" onkeyup="validate()">
    </form>
    .....Willy


  •  

    Posting Permissions

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