Wednesday, October 23, 2013

Jquery to Allow Alphanumeric in .net TextBox Control

Leave a Comment
Here in this article I have written the Jquery code to allow only alphabets and numbers in asp.net textbox control. Use this jquery script in the cases where you don't want to allow any other special characters except alphabets and numeric.

Jquery Code to Allow alpha-numeric:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {

            $(function () {
                $('#txtname').keydown(function (e) {
                    var pressedkey = e.keyCode;
                 
                      
                    if (e.shiftKey || e.ctrlKey || e.altKey) {
                        e.preventDefault();
                    } else {
                        var key = e.keyCode;
                        if (!((pressedkey == 8) || (pressedkey == 46) ||(pressedkey>=65 && pressedkey<=90)|| (pressedkey >= 35 && pressedkey <= 40) || (pressedkey >= 48 && pressedkey <= 57) || (pressedkey >= 96 && pressedkey <= 105))) {
                            e.preventDefault();
                        }
                    }

                });


            });

        });


    </script>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:TextBox runat="server" ID="txtname"></asp:TextBox>


        </div>
    </form>
</body>
</html>

0 comments:

Post a Comment