asp.net textbox only allows the method of input numbers to summarize _ practical tips

Source: Internet
Author: User

Copy Code code as follows:

<asp:textbox id= "TextBox1" onkeyup= "if (isNaN) execcommand (' Undo ')" runat= "Server"

Width= "80px" onafterpaste= "if (isNaN (value)) ExecCommand (' Undo ')" ></asp:textbox>


In fact, server controls can also add onkeydown and up events such as

That's it. You can only enter decimals and digits

In. NET development, in order to ensure the correctness of the data, often the user input to verify the content, the analogy is that only the number of input.

First, add a property event to the TextBox control:

Copy Code code as follows:

<asp:textbox class= "Text"

Onkeypress= "if (Event.keycode < | | | Event.keycode >57) Event.returnvalue = false;"

Id= "Txty_revenue" style= "Text-align:right" runat= "Server" width= "90%" maxlength= ">"

</asp:textbox>


Check if the keyboard is pressed 0-9, if not, do not put the current input into the text box

Note: This method controls the textbox to enter only numbers: 0~9, providing a way of thinking

Add:

1. The dashed box when the Cancel button is pressed

Add attribute value in input hidefocus or hidefocus=true

2. Read-only text box contents

Add attribute value in input readonly

3. Prevent back-emptying of the text document (you can make the style content as a class reference)


Copy Code code as follows:

<input Style=behavior:url (#default #savehistory); Type=text id=opersistinput>

The 4.ENTER key allows the cursor to move to the next input box
Copy Code code as follows:

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

5. Only for Chinese (with flashing)
Copy Code code as follows:

<input onkeyup= "Value=value.replace (/[-~]/g, '") "onkeydown=" if (event.keycode==13) event.keycode=9 "> Use the range of ASCII code to judge

6. Only for numbers (with flashing)
Copy Code code as follows:

<input onkeyup= "Value=value.replace (/[^\d]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ') "> Use the range of ASCII code to judge

7. Only for numbers (no flashing)
Copy Code code as follows:

<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 the range of ASCII code to judge

8. Can only input English and digital (with flashing)
Copy Code code as follows:

<input onkeyup= "Value=value.replace (/[\w]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ') "> verify using JS Regular expression

9. Shielding Input Method
Copy Code code as follows:

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

10. Only digits, decimal points, minus (-) characters (no flashing) can be entered.
Copy Code code as follows:

<input onkeypress= "If" (event.keycode!=46 && event.keycode!=45 && (event.keycode<48 | | event.keycode>57) Event.returnvalue=false "> Using the range of ASCII code to judge

11. Can only enter two decimal places, three decimal places (with flashing)
Copy Code code as follows:

<input maxlength=9 onkeyup= "If" (Value.match (/^\d{3}$/)) Value=value.replace (Value,parseint (VALUE/10)) value= Value.replace (/\.\d*\./g, '. ') "onkeypress=" if (event.keycode<48 | | event.keycode>57) && Event.keycode !=46 && event.keycode!=45 | | Value.match (/^\d{3}$/) | | /\.\d{3}$/.test (value)) {Event.returnvalue=false} "Id=text_kfxe name=text_kfxe> uses the regular expression of JS to authenticate.

In fact, do not limit the user's input in the application, as long as the user to verify the input can be, because this limit often brings a bad experience

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.