Avoid special characters and space to enter in textbox using Jquery

In this article I am going to explain how to avoid special characters and space to enter (type) in textbox using Jquery

Description:
In application I am validating username. I want to avoid entering space and special characters in username. I am going to do this using jquery.

Implementation:
We have to create Keypress method. Here is jquery code that I am using:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
            <script type="text/javascript">
                $(document).ready(function () {
                    // restrict special characters
                    $('#TextBox1').keypress(function (key) {
                        var regexpns = new RegExp("^[a-zA-Z0-9]+$");
                        var key = String.fromCharCode(event.charCode ? event.which : event.charCode);
                        if (!regexpns.test(key)) {
                            event.preventDefault();
                            document.getElementById("message").style.display = 'block';
                            return false;                       
                        }
                        else
                        {
                            document.getElementById("message").style.display = 'none';
                        }
                    });
                });
            </script>

Complete HTML Markup of webform:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
            <script type="text/javascript">
                $(document).ready(function () {
                    // restrict special characters
                    $('#TextBox1').keypress(function (key) {
                        var regexpns = new RegExp("^[a-zA-Z0-9]+$");
                        var key = String.fromCharCode(event.charCode ? event.which : event.charCode);
                        if (!regexpns.test(key)) {
                            event.preventDefault();
                            document.getElementById("message").style.display = 'block';
                            return false;                       
                        }
                        else
                        {
                            document.getElementById("message").style.display = 'none';
                        }
                    });
                });
            </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td>Enter Username :</td>
                 <td><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <label id="message" style="color: Red; display: none">Special Characters and space not allowed</label></td>
            </tr>
        </table>       
    </div>
    </form>
</body>
</html>



Post a Comment

[blogger]

MKRdezign

Contact Form

Name

Email *

Message *

Powered by Blogger.
Javascript DisablePlease Enable Javascript To See All Widget