user1547554
user1547554

Reputation: 591

ASP.net: TextBox allows out of the range numbers when typed through keyboard in TextMode = "Number"

I want a TextBox value between 1 and 99 with NumericUpDown in ASP.net webforms. (without using NumericUpdown Extended Ajax Control Toolkit because my client doesn't allow that). So, for that I'm using the following code:

<asp:TextBox TextMode="Number" runat="server" min="1" max="99" step="1" ID="tbRooms" />

This works fine when I use up and down arrow keys, it ranges only between 1 and 99 as expected. However, when I type the values from the keyboard, it takes any random value, let's say 1000. I tried to put MaxlLength = "2" but that didn't work out. So, how can I resolve the issue, so that it doesn't allow any random number even through the keyboard.

Thanks.

Upvotes: 3

Views: 22184

Answers (2)

Tarik Onal
Tarik Onal

Reputation: 1

onKeyPress add this code:

onKeyPress="if(this.value.length==2) return false;"

Upvotes: 0

Shibu Thomas
Shibu Thomas

Reputation: 3196

Please try this one:

<table border="0" cellpadding="0" cellspacing="0">
   <tbody>
      <tr>
         <td>
            <b>Enter Value</b>
            <asp:textbox runat="server" id="txtRangeValidator">
            </asp:textbox>
         </td>
      </tr>
      <tr>
         <td>
            <asp:rangevalidator ID="Rangevalidator1" errormessage="Please enter value between 1-99." forecolor="Red" controltovalidate="txtRangeValidator" minimumvalue="1" maximumvalue="99" runat="server" Type="Integer">
            </asp:rangevalidator>
         </td>
      </tr>
   </tbody>
</table>

Upvotes: 5

Related Questions