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

    Enter only 10 digits in text box in

    hi friends,

    how to enter only 10 digits in text box in

    not maximum or minimum.

    please give the solution,

  • #685151
    hi use TextBox1.Text.Length property to check whether it has 10 values or not if not display error.
    Rayala HariKishore achieved it.

  • #685156
    use Regx in Javascript to achive this on key press.

    expression ="^[0-9]{10}"

    it's allow u to enter only 10 digit muneric value.

  • #685160
    please give in detail,

    i am not understand.

  • #685161

    You can use the Regular Expression validator to restrict the user to enter 10 digits

    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ValidationExpression="^\d{10}$" ErrorMessage="Error Message"></asp:RegularExpressionValidator>


    Thanks & Regards,
    Mahesh Durgam

  • #685165
    You can set the maximum length of the text box as below.

    <asp:TextBox ID="Value1"

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #685171

    its working.

  • #685172
    try this,

    <asp:Label ID="lblphno" runat="server" Text="Mobile Number"></asp:Label>
    <asp:TextBox ID="txt_phno" MaxLength="10" runat="server" TabIndex="6" > </asp:TextBox>
    //textbox control with maximum limit of 10 digits
    <asp:RequiredFieldValidator ID="Rfvphno" runat="server" ControlToValidate="txtqty"
    ErrorMessage="Enter the Phone Number" ForeColor="Red" SetFocusOnError="True"
    // require field validator for checking the field is empty means display the error message
    <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="txt_phno"
    ErrorMessage="Please Enter Only Numbers" ValidationExpression ="^\d+$" ValidationGroup="valnewmaterial">*</asp:RegularExpressionValidator>
    // regular expression for user enters alpha char means diplay the error message.

  • #685174
    you can use it in two ways one is using c code using the property of textbox and other is using using regular expression (in this you can use regularexpression validator or js code.)

    first take the textbox and put the regularexpressionvalidator control and goto its properties and then choose validationexpression and there you put \d[10] it means it checks for length is 10 or not. done

    second is you can use in cs code that is when you are leaving the control means when focus is leaving the control you can fire event and check whether the length is 10 or not or while submitting it's length is 10 or not. suppose submit in click event use the if(Textbox1.Text.Length != 10 ) {"length must be 10");} like this you can use and for leaving the focus it is done in windows application here you can use textchanged event for this use same length property
    you can choose any way.
    any way but for client side validation use js code only. means use the regular expression.

    Rayala HariKishore achieved it.

  • 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.