Showing posts with label Restrict Special Characters. Show all posts
Showing posts with label Restrict Special Characters. Show all posts

Friday 28 March 2014

Restrict Special Characters in TextBox using JavaScript in asp.net

Hi friends, today i am going show you how to Restrict Special Characters in TextBox or in Textarea  using JavaScript in asp.net.

Restrict Special Characters in TextBox using JavaScript in asp.net
Screen 1
I have two method to do it in javascript.

First Method:

In this method you only want to add "RestrictHtmlCharacter-Tags.js" file in order to restrict special character in textbox.

Using this method you don't need to add anything to textbox. Only you have to do one thing only call that "RestrictHtmlCharacter-Tags.js" js file in your page and it will restrict all the textbox entering special character. For this you have to add jquery 1.7.2.min.js

Click here to download RestrictHtmlCharacter-Tags.js

If you have update panel on your page please add following initializing code to initialize the  "RestrictHtmlCharacter-Tags.js".

        //Restrict HTML Character
        $(document).ready(function() {
            try {
                RestrictHtmlCharacter();                Sys.WebForms.PageRequestManager.getInstance().add_endRequest(RestrictHtmlCharacter);
            }
            catch (err) {
            }
        }); 

 Add this following file in head tag:

<script src="Script/jquery-1.7.2.min.js" type="text/javascript"></script>

<script src="Script/RestrictHtmlCharacter-Tags.js" type="text/javascript"></script>

 Using this way you don't need too add anything in textbox only add this javascript file.

Second Method:

The second method to do the same is following:

The script works in such a way that the TextBox will accept only alphabets, numbers i.e. alphanumeric values with some allowed special keys, thus unless a special character key has been specified to be excluded it won’t be accepted.

HTML:

<div align="center" style="width: 100%;">
        <div style="width: 300px;">
            <fieldset>
                <legend>Restrict Special Character</legend>
                <br />
                Note: Special character <b>not</b> allowed.
                <br />
                <br />
                <asp:TextBox ID="txtSearch" runat="server" onkeypress="return IsCharacterRestrict(event);"
                    ondrop="return false;" onpaste="return false;" Style="font-size: 14px;
                    margin-top: 10px; height: 30px; width: 250px; color: #353535;" MaxLength="100"></asp:TextBox>
                <br />
                <br />
                <br />
            </fieldset>
        </div>
    </div>

Script:

<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 IsCharacterRestrict(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));
            return ret;
        }
    </script>
 

Download Link :

Download Complete Project

Enjoyed this post? Share and Leave a comment below, thanks! :)