I want to allow only english characters and numbers in html input field (A-Z, a-z, 0-9 are allowed). I don't want that someone to insert special characters with copy/paste.
Asked
Active
Viewed 6,657 times
-2
-
1http://stackoverflow.com/questions/1995521/jquery-js-allow-only-numbers-letters-in-a-textfield – Shrinivas Pai Jul 21 '15 at 08:50
-
possible duplicate of [How to allow only numeric (0-9) in HTML inputbox using jQuery?](http://stackoverflow.com/questions/995183/how-to-allow-only-numeric-0-9-in-html-inputbox-using-jquery) – galath Jul 21 '15 at 08:54
3 Answers
3
If you are using new browser you can use pattern
attribute like
<input type="text" pattern="[A-Za-z0-9]" required/>
or you can use js like
$("#id").keypress(function(event){
var ew = event.which;
if(48 <= ew && ew <= 57)
return true;
if(65 <= ew && ew <= 90)
return true;
if(97 <= ew && ew <= 122)
return true;
return false;
});

Vidya Sagar
- 1,699
- 3
- 17
- 28
2
Check this pattern="^[\x20-\x7F]+$
"
At me this it work:D
<input type="text" pattern="^[\x20-\x7F]+$" >

Henryk Antoni Panas
- 50
- 4
0
In JavaScript, create a conditional checking for ASCII values. A way to do this is
var character
will represent one character of the array of characters entered into the input field.
character.charCodeAt(0);
will turn it into an ASCII value. Then just use < than and > than symbols to check if the ASCII values are in range of letters and numbers.

Ben Adamsky
- 86
- 8