By 06/02/2014 15 Comments

How To Allow Only Alphanumeric Characters in an ASP.NET TextBox using jQuery

Only alphanumeric characters are allowed

  • We create a website which lets users to subscribe for news letters by entering their valid emails
  • We do not want any invalid symbol / character (e.g $*&()>?@|/\) entered by visitors in textbox
  • It can be easily achieved in asp.net using JQuery

The code shown bellow matches a regular expression /[^a-zA-Z0-9 ]/g that finds all characters that are not alphabets or numbers. If the user enters  a character that  is not an  alphabet or  number, the character is immediately replaced with an empty string.

Jquery Code snippet

<script type="text/javascript" src="Scripts/jquery-1.8.2.js"></script> <!--- import jquery library --->
    <script>
        $(document).ready(function() { // method called, when document loaded by browser

            $('[id$="TT_TextBox1"]').bind('keyup blur', function() { // TT_TextBox1 called an method bind(), bind() method attaches one or more event handler for it, here bind() function attaches 2 event handlers keyup and blur, also pass un unknown function which will be called when these events will be fired

                if (this.value.match(/[^a-zA-Z0-9 ]/g)) { // check if user enter invalid characters
                    this.value = this.value.replace(/[^a-zA-Z0-9 ]/g, ''); // if true, replace those invalid characters into an space (specifies as a second argument from replace method)
                }

            });
            });

    </script>

ASP.NET Code snippet

<!--- Following code renders textbox and label control on browser --->

<asp:Label runat="server" Text="Try entering Non alphanumeric char"></asp:Label>

        <asp:TextBox ID="TT_TextBox1" runat="server" Text="" />






Posted in: ASP.NET, jQuery

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .