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>
Yeyeye
  • 19
  • 10
  • 1
    Have you tried `ControlToValidate="txt" ValidationExpression="\d{,10}"`? – Andrew Morton Jul 13 '22 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}" – Yeyeye Jul 13 '22 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](https://stackoverflow.com/a/18458933/1115360). If you also want to prevent pasting in non-digits, I'm sure you can do some research for that. – Andrew Morton Jul 13 '22 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. – Yeyeye Jul 13 '22 at 19:02
  • You could try using `TextMode` instead of `Type`: [Render ASP.NET TextBox as HTML5 Input type "Number"](https://stackoverflow.com/a/49859912/1115360), but it might not make any difference. – Andrew Morton Jul 13 '22 at 19:29
  • There's [this incomplete answer](https://stackoverflow.com/a/50092356/1115360) to deal with the paste issue. It may be simpler to just tell the user that the input data is invalid and what is required: the user should soon tire of trying to enter incorrect data. – Andrew Morton Jul 13 '22 at 19:35
  • TextMode did the same thing Type did , no 10 digit restriction . – Yeyeye Jul 13 '22 at 21:37
  • the warning sign shows but just knowing users will still search which spits out a yellow box which is what I am trying to prevent. – Yeyeye Jul 13 '22 at 21:38

2 Answers2

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>
Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51
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>
Yeyeye
  • 19
  • 10