Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript validation: Block special characters

How can I restrict users from entering special characters in the text box. I want only numbers and alphabets to be entered ( Typed / Pasted ).

Any samples?

like image 984
Shyju Avatar asked May 25 '09 10:05

Shyju


1 Answers

Try this one, this function allows alphanumeric and spaces:

function alpha(e) {
    var k;
    document.all ? k = e.keyCode : k = e.which;
    return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 || k == 32 || (k >= 48 && k <= 57));
}

in your html:

<input type="text" name="name"  onkeypress="return alpha(event)"/>
like image 138
qbtrance.com Avatar answered Oct 21 '22 03:10

qbtrance.com