Tuesday, October 22, 2013

Jquery to allow only alphabets(letters) in TextBox Control of .net

Leave a Comment
Jquery can be used to limit the text box control to allow only alphabets and prevent all other types of characters. To do this use the keydown event of textbox control in jquery and prevent all characters and symbols excepts alphabets(a-z).

Jquery Code to Allow only Alphabets:

<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 (!((pressedkey == 8) || (pressedkey == 32) || (pressedkey == 46) || (pressedkey >= 35 && pressedkey <= 40)||(pressedkey==17)||(pressedkey==18)||(pressedkey==19) || (pressedkey >= 65 && pressedkey <= 90))) {
                        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