SoFunction
Updated on 2025-03-07

Summary of the method that TextBox allows only input numbers

Copy the codeThe code is as follows:

<asp:textbox 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.

That's all, you can enter decimals and numbers.

In .net development, in order to ensure the correctness of data, the content entered by users is often required to be verified, which is a metaphor that only numbers can be entered.

First add a property event to the TextBox control:

Copy the codeThe code is as follows:

<asp:textbox class="Text"

        onkeypress="if ( < 48 || >57) = false;"

style="TEXT-ALIGN: right" runat="server" Width="90%" MaxLength="12">

    </asp:textbox>


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

Note: This method controls TextBox to enter only numbers: 0~9, providing an idea

Replenish:

1. The dotted box when the cancel button is pressed

Add attribute value hideFocus or HideFocus=true in input

2. Read-only text box content

Add attribute value in input readonly

3. Prevent TEXT documents from being cleared after retreat (the style content can be used as a class reference)


Copy the codeThe code is as follows:

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

The key moves the cursor to the next input box
Copy the codeThe code is as follows:

<input onkeydown="if(==13)=9" >

5. Only in Chinese (with flashing)
Copy the codeThe code is as follows:

<input onkeyup="value=(/[ -~]/g,'')" onkeydown="if(==13)=9">Use the range of Ascii code to make judgments

6. Only numbers (with flashes)
Copy the codeThe code is as follows:

<input onkeyup="value=(/[^\d]/g,'') "onbeforepaste="('text',('text').replace(/[^\d]/g,''))">Use the range of Ascii code to make judgments

7. Only numbers (no flashing)
Copy the codeThe code is as follows:

<input style="ime-mode:disabled" onkeydown="if(==13)=9" onKeyPress="if ((<48 || >57)) =false">Use the range of Ascii code to make judgments

8. Only enter English and numbers (with flashing)
Copy the codeThe code is as follows:

<input onkeyup="value=(/[\W]/g,'') "onbeforepaste="('text',('text').replace(/[^\d]/g,''))">Use js regular expressions for verification

9. Blocking input method
Copy the codeThe code is as follows:

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

10. Only enter numbers, decimal points, minus signs (-) characters (no flash)
Copy the codeThe code is as follows:

<input onKeyPress="if (!=46 && !=45 && (<48 || >57)) =false">Use the range of Ascii code to make judgments

11. Only two decimal places and three decimal places (with flash)
Copy the codeThe code is as follows:

<input maxlength=9 onkeyup="if((/^\d{3}$/))value=(value,parseInt(value/10)) ;value=(/\.\d*\./g,'.')" onKeyPress="if((<48 || >57) && !=46 && !=45 || (/^\d{3}$/) || /\.\d{3}$/.test(value)) {=false}" id=text_kfxe name=text_kfxe> Use js regular expression for verification.

In fact, do not restrict user input in the application, just verify user input, because this restriction often brings a bad experience to people.