Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I don't want to allowed only the special character like < > and &

What I have tried:

and here's the code that i search on the internet but this code did not accept all of the special character.
HTML
<pre><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" onkeypress="return IsAlphaNumeric(event);" ondrop="return false;"
         />
    <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
        function IsAlphaNumeric(e) {
            var keyCode = e.keyCode == 1 ? 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
Updated 25-Oct-17 4:00am
v2

1 solution

Try this function instead:
JavaScript
function IsAlphaNumeric(e) {
        var keyCode = e.keyCode == 1 ? e.charCode : e.keyCode;
        var ret = ((keyCode > 31 && keyCode < 127) && keyCode != 38 && keyCode != 60 && keyCode != 62);
            document.getElementById("error").style.display = ret ? "none" : "inline";
            return ret;
}
It will allow any character with ASCII code[^] from 32 to 126 apart from < and > and &

YOu can easily add or exclude others.

Also, just to add: do not rely on JavaScript (client side) validation for user input. You must always run the same checks in code (VB, C#, whatever) on the server as well.
 
Share this answer
 
v2
Comments
Member 13427032 27-Oct-17 8:07am    
Thank you so much this code is working and i will be adding a validation code in c#. Thanks again
Member 13427032 3-Nov-17 15:05pm    
hi sir good day, i add keyCode == 13 but the enter still is not working. and i check on the ASCII code the value of the enter is not there. can you help me about the ENTER key
A_Griffin 3-Nov-17 15:13pm    
what do you expect to happen when you press enter? IT's a single line text box...
Member 13427032 3-Nov-17 15:58pm    
i have a textbox that is multiline and when i push the enter button it shows the error message (Special Characters not allowed) can you help me when pressing the enter it will add a new line on the textbox. Please
A_Griffin 3-Nov-17 16:20pm    
Just change the one line to:
var ret = (keyCode == 13) || (((keyCode > 31 && keyCode < 127) && keyCode != 38 && keyCode != 60 && keyCode != 62));

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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