You must Sign In to post a response.
  • Category: WPF

    How to make textbox to accept only characters

    Hi all,

    I need to make the textbox field to accept only characters, no numeric or other special characters should be entered. If numbers or other special character is entered in the textbox it should show the error message.






    Regards,
    Mohanraj
  • #652207
    function IsNumeric(sText,obj)
    {
    var ValidChars = "0123456789";
    var IsNumber=true;
    var Char;
    var sVAL
    Char = sText.charAt(0);
    if(Char=="." && obj.value.indexOf('.')>-1)
    IsNumber = false;
    else
    {
    if (ValidChars.indexOf(Char) == -1)
    {
    IsNumber = false;
    }
    }
    return IsNumber;
    }

    THE SERVER CONTROL IS:
    <asp:TextBox ID="TextBox2" runat="server" Height="71px" onkeypress="return IsNumeric(String.fromCharCode(event.keyCode),this);" TextMode="MultiLine" Width="396px"></asp:TextBox>

  • #652209
    Please check the following code:


    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
    <title>Untitled Page</title>

    <script type="text/javascript">
    function isNumberKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if ((charCode < 97 || charCode > 122) && (charCode < 65 || charCode > 90))
    return false;
    return true;
    }

    </script>

    </head>
    <body>
    <form id="form1" runat="server">
    <asp:TextBox ID="TextBox2" runat="server" Style="z-index: 102; left: 151px; position: absolute;
    top: 169px" onkeypress="return isNumberKey(event)"></asp:TextBox>
    </form>
    </body>
    </html>


    Thanks & Regards
    Paritosh Mohapatra
    Microsoft MVP (ASP.Net/IIS)
    DotNetSpider MVM

  • #652213
    Hi,

    Check below JavaScript functions to block numeric values,

    Method 1

    <script language="javascript">
    function keypress()
    {
    if(((event.keyCode < 48)||(event.keyCode > 57)))
    {
    event.returnValue=false;
    }
    }
    </script>


    Method 2


    <script language ="javascript">
    function NumTest()
    {var val = document.getElementById('<%=TextBox1.ClientId %>').value;
    if (!isNaN(val))
    {
    alert("Numeric values are not alloweded");
    return (false);
    }

    }
    </script>


    Regards,
    Asheej T K

  • #652214
    You can try this Regular expression validator for the same validation




    <asp:RegularExpressionValidator ID="regexpName" runat="server"
    ErrorMessage="This expression does not validate."
    ControlToValidate="txtName"
    ValidationExpression="^[a-zA-Z\s]+$" />

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #652315
    Use javascript on key press like this


    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Allow alpha only</title>
    <script type="text/javascript">
    function isCharKey(evt) {
    alert(document.getElementById("TextBox1").value.length());
    var charCode = (evt.which) ? evt.which : event.keyCode
    alert(charCode);
    if (charCode > 64 && charCode < 91 || charCode > 96 && charCode < 123 || charCode == 8 || charCode == 127 )
    return true;
    else
    return false;
    }
    </script>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    Enter password <asp:TextBox ID="TextBox1" runat="server" onkeyPress="return isCharKey(event)"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </div>
    </form>
    </body>
    </html>

    Regards
    N.Ravindran
    Your Hard work never fails


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.