0

Issue : how to restrict textbox to only allow 10 digits using type = number and hide the arrow on a aspx file Requirements:

  • no more than 10 digits in the box
  • allows copy and paste but filters out letters in it for eg 1234a or a1234
  • allows only numbers

    <asp:TextBox ID="txt" runat="server" IsReadOnly="false" Type="Number" Maxlength="10" ></asp:TextBox>
    <asp:RegularExpressionValidator ID="RegularExpression1" runat="server" ControValidator="txt" ValidationExpression="\d+" ErrorMessage="only numeric values allowed."></asp:RegularExpressionValidator>

8
  • 1
    Have you tried ControlToValidate="txt" ValidationExpression="\d{,10}"? Commented Jul 13, 2022 at 18:05
  • without Type = Number , validationExpression = "\d{,10}" works . But it does NOT resolve all the listed requirements: -no letters allowed -only numbers up to 10 digits -should allow copy and paste Here are all the options that I have tried : - \d+$ - pattern = "[0-9]{10}" Commented Jul 13, 2022 at 18:40
  • Are you saying that you want it to only allow digits to be entered? If so: asp.net validation to make sure textbox has integer values. If you also want to prevent pasting in non-digits, I'm sure you can do some research for that. Commented Jul 13, 2022 at 18:49
  • Thank you so much . The above solution , does not allow letter input but allows letters if it is copy and paste. it should filter out a letter in the instance of a copy paste. Commented Jul 13, 2022 at 19:02
  • You could try using TextMode instead of Type: Render ASP.NET TextBox as HTML5 Input type "Number", but it might not make any difference. Commented Jul 13, 2022 at 19:29

2 Answers 2

0

This works:

        <asp:TextBox ID="TextBox1" runat="server"  
            TextMode="Number" Width="187px"
            onkeypress="return this.value.length<=10" >
        </asp:TextBox>

If you need to hide the up/down (spinner), then use this:

<style>

  .myspinhide 
  /* Chrome, Safari, Edge, Opera */
  input::-webkit-outer-spin-button,
  input::-webkit-inner-spin-button 
  {-webkit-appearance: none; margin: 0;}

   /* Firefox */
   .myspinhide input[type=number] {-moz-appearance: textfield;} 
</style>

        <asp:TextBox ID="TextBox1" runat="server"  CssClass="myspinhide"
            TextMode="Number" Width="236px"
            onkeypress="return this.value.length<=10"  >
        </asp:TextBox>
Sign up to request clarification or add additional context in comments.

Comments

0

This is worked as well

<asp TextBox ID="txtId" width = "205px" runat="server" MaxLength="10"/>
<asp:RangeValidator runat="server" ID = "valRangeTxtcontactId" controlToValidate="txtId" Type ="Integer" Minimumvalue = "0" Maximumvalue ="2147483647" CssClass="input-error" ErrorMessage="Integers only" Display="Dynamic"></asp:RangeValidator>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.