Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
To allow letters and spaces only
Posted

user below javascript function:-
C#
// To accept only character  and spaces.
function IsCharacterKeyCheck(event)
{
    var charCode = (event.which) ? event.which : event.keyCode;
    if (charCode >= 65 && charCode <= 90 || charCode >= 97 && charCode <= 122 || charCode == 8 || charCode == 32)
    return true;
    return false;
}


and add
onkeypress="return IsCharacterKeyCheck(event)"
in your asp:button.
 
Share this answer
 
Comments
Karthik_Mahalingam 15-Jan-14 6:39am    
5!i have used your charcode conditions in my jquery solution :)
TrushnaK 15-Jan-14 6:45am    
welcome...
On top of Trushnak solution..
Try this using jquery

XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script src="jquery-1.10.2.js" type="text/javascript"></script>
    <script type="text/javascript">

        $(function () {

            $('#txt').keydown(function (event) {
                var charCode = (event.which) ? event.which : event.keyCode;
                var isvalid = charCode >= 65 && charCode <= 90 || charCode >= 97 && charCode <= 122 || charCode == 8 || charCode == 32;
                if (!isvalid)
                    event.preventDefault();

            });

        });

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:TextBox ID="txt" runat="server"></asp:TextBox>
    </form>
</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900