Home > Articles > .Net Framework Articles > How to prevent input field from special character.

How to prevent input field from special character.

by vikash kumar   on Jan 28, 2016   Category: .Net Framework  | Level: Beginner  |  Views: 663    |  Points: 100   
Like this article? Bookmark and Share:
Preventing input fields from special characters.

Put these script codes under head section of the page.

 

<script language="javascript" type="text/javascript">

        function check(e) {

            var keynum

            var keychar

            var numcheck

            // For Internet Explorer

            if (window.event) {

                keynum = e.keyCode;

            }

                // For Netscape/Firefox/Opera

            else if (e.which) {

                keynum = e.which;

            }

            keychar = String.fromCharCode(keynum);

            //List of special characters you want to restrict

            if (keychar == "'" || keychar == "`" || keychar == "!" || keychar == "@" || keychar == "#" || keychar == "$" || keychar == "%" || keychar == "^" || keychar == "&" || keychar == "*" || keychar == "(" || keychar == ")" || keychar == "-" || keychar == "_" || keychar == "+" || keychar == "=" || keychar == "/" || keychar == "~" || keychar == "<" || keychar == ">" || keychar == "," || keychar == ";" || keychar == ":" || keychar == "|" || keychar == "?" || keychar == "{" || keychar == "}" || keychar == "[" || keychar == "]" || keychar == "¬" || keychar == "£" || keychar == '"' || keychar == "\\") {

                return false;

            } else {

                return true;

            }

        }

    </script>


And Put these HTML codes in body section of the page and see the result.

User id : <input type="text" id="txtname" name="txtname" onkeypress="return check(event)"/>


Happy coding......


Like this article? Bookmark and Share:

Most viewed Articles

User Comments


No response found, be the first to review this article.

Submit feedback about this article

Please sign in to post feedback

Latest Posts