Asp.net's TextBox only allows you to enter numeric method summaries

  • 2020-05-27 04:43:22
  • OfStack


<asp:textbox id="TextBox1" onkeyup="if(isNaN(value))execCommand('undo')" runat="server"
Width="80px" onafterpaste="if(isNaN(value))execCommand('undo')"></asp:textbox>

In fact, the server control can also add onkeydown and up events

You can only enter decimals and Numbers

In the development of.net, in order to ensure the correctness of the data, users are often required to input content to verify, which means that only Numbers can be input.

First, add a property event to the TextBox control:


<asp:textbox class="Text"
        onkeypress="if (event.keyCode < 48 || event.keyCode >57) event.returnValue = false;"
id="txtY_Revenue" style="TEXT-ALIGN: right" runat="server" Width="90%" MaxLength="12">
    </asp:textbox>

When the keyboard is pressed, check whether it is 0-9. If not, do not put the current input into the text box

Note: this method controls TextBox input only Numbers: 0~9, provide a way of thinking

Supplement:

1. The dotted box when the cancel button is pressed

Add the attribute value hideFocus or HideFocus=true to input

2. Read only the text box

Add the property value readonly to input

3. Avoid backstepping empty TEXT documents (think of style content as a class reference)



<INPUT style=behavior:url(#default#savehistory); type=text id=oPersistInput>

4. The ENTER key moves the cursor to the next input box

<input onkeydown="if(event.keyCode==13)event.keyCode=9" >

5. Chinese only (flashing)

<input onkeyup="value=value.replace(/[ -~]/g,'')" onkeydown="if(event.keyCode==13)event.keyCode=9"> using Ascii Judge the range of the code 

6. Number only (flashing)

<input onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"> using Ascii Judge the range of the code 

7. Number only (no flashing)

<input style="ime-mode:disabled" onkeydown="if(event.keyCode==13)event.keyCode=9" onKeyPress="if ((event.keyCode<48 || event.keyCode>57)) event.returnValue=false"> using Ascii Judge the range of the code 

8. English and numerals only (flashing)

<input onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"> using js The regular expression of 

9. Screen input method

<input type="text" name="url" style="ime-mode:disabled" onkeydown="if(event.keyCode==13)event.keyCode=9">

10. Can only input digits, decimal point, minus (-) character (no flashing)

<input onKeyPress="if (event.keyCode!=46 && event.keyCode!=45 && (event.keyCode<48 || event.keyCode>57)) event.returnValue=false"> using Ascii Judge the range of the code 

11. Only two decimal places and three decimal places (flashing) can be input.

<asp:textbox class="Text"
        onkeypress="if (event.keyCode < 48 || event.keyCode >57) event.returnValue = false;"
id="txtY_Revenue" style="TEXT-ALIGN: right" runat="server" Width="90%" MaxLength="12">
    </asp:textbox>
0

In fact, in the application do not limit the user's input, just verify the user's input is ok, because this limit often brings people a bad experience


Related articles: