Click here to Skip to main content
14,826,949 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i want to restrict user specific characters like # | % i have supplied asci code of those characters but not working

What I have tried:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
    <style type="text/css">
            font-size: 9pt;
            font-family: Arial;
    Alphanumeric value:
    <input type="text" id="text1" />
    <span id="error" style="color: Red; display: none">* Special Characters not allowed</span>
    <script type="text/javascript">
        var specialKeys = new Array();
		specialKeys.push(8); //Backspace
        specialKeys.push(9); //Tab
        specialKeys.push(46); //Delete
        specialKeys.push(36); //Home
        specialKeys.push(35); //End
        specialKeys.push(37); //Left
        specialKeys.push(39); //Right
		specialKeys.push(92); //\|
        function IsAlphaNumeric(e) {
            var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode;
            var ret = ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122) || (specialKeys.indexOf(e.keyCode) != -1 && e.charCode != e.keyCode));
            document.getElementById("error").style.display = ret ? "none" : "inline";
            return ret;
Karthik_Mahalingam 1-Apr-16 6:46am
what is the issue
Rakib Ahmed 1-Apr-16 6:56am
i want to restrict user specific characters like # | % i have supplied asci code of those characters but not working
Karthik_Mahalingam 1-Apr-16 7:05am
little corrections made to your mark up, check my solution.

try this

    <input type="text" onkeypress="return IsAlphaNumeric(event);" id="text1" />
    <b id="error" style="display:none; color:red">Invalid</b>

Rakib Ahmed 1-Apr-16 7:27am
but when i write code for exclude += specialKeys.push(61); //+= is not working but for backspace it is working
Rakib Ahmed 1-Apr-16 7:30am
i mean you see that specialKeys.push(8); //Backspace it is working fine
but when i add specialKeys.push(61)//+= is not working
Karthik_Mahalingam 1-Apr-16 7:40am
+= is a special character right
Rakib Ahmed 1-Apr-16 7:43am
what can i do for this situation?
Karthik_Mahalingam 1-Apr-16 7:45am
need to tune your allow exceptions.
Karthik_Mahalingam 1-Apr-16 7:51am
for + and = the keycode is 43 and 45
Rakib Ahmed 1-Apr-16 7:54am
bro its says invalid like other special characters but it should work like the backspace
Karthik_Mahalingam 1-Apr-16 7:55am
which browser?
Rakib Ahmed 1-Apr-16 7:58am
Karthik_Mahalingam 1-Apr-16 8:08am
Find the key code for mozilla
Karthik_Mahalingam 1-Apr-16 8:35am
what error mesage you are getting?
do you have team viewer ?
A really standard way to block input of a character is the function event.preventDefault:[^].

See also my past answer: how to delete the character typed in key press event[^].

This short manual can also be useful: Keyboard Text Input Filtering: Examples[^].

Be sure to allow backspace, which is also considered as a character with the code point 8.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900