Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate Keystrokes

Status
Not open for further replies.

blasterstudios

Technical User
Dec 30, 2004
128
US
I want to validate keystrokes and only allow lowercase letters, numbers, underscore, and dash. No spaces, no capital letters, and no special characters or symbols. I want it to check my form on the fly as well. So like on the onKeyPress event, it checks it. How do i go about doing this?

Here's what I tried, but it doesn't work:
Code:
function letternumber(e)
{
var key;
var keychar;

if (window.event){
   key = window.event.keyCode;
else if (e)
   key = e.which;
}

if ((key==null) ||
	((key>=97) && (key<=122)) ||
	((key>=48) && (key<=57)) ||
	(key==95) || (key==45) || (key==97))
   return true;
else
   return false;
}
 
[tt]>if (window.event){
> key = window.event.keyCode;
>else if (e)
> key = e.which;
>}
[/tt][tt]
if (window.event){
key = window.event.keyCode;
[red]}[/red]
else if (e)
[red]{[/red]
key = e.which;
}
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top