Click here to Skip to main content
14,826,929 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:

HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style type="text/css">
        body
        {
            font-size: 9pt;
            font-family: Arial;
        }
    </style>
</head>
<body>
    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;
        }
    </script>
</body>
</html>
Posted
v2
Comments
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.

A really standard way to block input of a character is the function event.preventDefault:
https://developer.mozilla.org/en-US/docs/Web/API/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.

—SA
   
try this

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

</body>
   
Comments
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 code.to allow exceptions.
Karthik_Mahalingam 1-Apr-16 7:51am
   
for + and = the keycode is 43 and 45
specialKeys.push(43);
specialKeys.push(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
   
mozila
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 ?

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