View Full Version : onKeyPress function

12-29-2003, 09:06 AM
Hey, I have a function, like so:

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


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??

Willy Duitt
12-29-2003, 02:28 PM
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:

<script type="text/javascript">
function validate(){
name = document.form1.Username.value;
name_re = new RegExp("^[a-zA-Z '.-]+$", "g");
return true;
alert('This is not a valid character for a name');
name = name.substring(0,name.length-1);
document.form1.Username.value = name;

<form name="form1">
<input type="text" name="Username" onkeyup="validate()">