Asp.net textbox can only enter numbers

Source: Internet
Author: User

<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 events such as onkeydown and up.

In this way, only decimals and numbers can be entered.

 

In. NET development, to ensure data correctness, users often need to input content for verification. In the analogy, only numbers can be entered.

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 the press is 0-9. If not, do not put the current input into the text box.

Note: This method controls textbox to only input numbers: 0 ~ 9. Provide an idea

Supplement:

1. The dotted box when the cancel button is pressed

 

Add the attribute value hidefocus or hidefocus = true in input.

2. Read-only text box content

 

Add the attribute value readonly in input.

3. Prevent text files from being cleared (style content can be referenced as a class)

 

<Input style = behavior: URL (# default # savehistory); type = text id = opersistinput>

4. Press enter to move the cursor to the next input box.

<Input onkeydown = "If (event. keycode = 13) event. keycode = 9">

5. It can only be Chinese (with flashing)

<Input onkeyup = "value = value. Replace (/[-~] /G, '')" onkeydown = "If (event. keycode = 13) event. keycode = 9"> use the ASCII code range to determine

6. Only numbers are allowed (with flashing)

<Input onkeyup = "value = value. replace (/[^ \ D]/g, '')" onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text '). replace (/[^ \ D]/g, '')"> use the ASCII code range to determine

7. Only numbers are allowed (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 "> use the ASCII code range to determine

8. Only English and numbers can be entered (with flashing)

 

<Input onkeyup = "value = value. replace (/[\ W]/g, '')" onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text '). replace (/[^ \ D]/g, '')"> use the regular expression of JavaScript for verification.

9. shielded Input Method

<Input type = "text" name = "url" style = "ime-mode: Disabled" onkeydown = "If (event. keycode = 13) event. keycode = 9">

10. You can only enter numbers, decimal points, minus (-) characters (no flashing)

<Input onkeypress = "If (event. keycode! = 46 & event. keycode! = 45 & (event. keycode <48 | event. keycode> 57) event. returnvalue = false "> use the ASCII code range to determine

11. Only two decimal places can be entered, and three decimal places (with flashing)

<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> use the regular expression of Js for verification.

 

In fact, in applications, do not restrict user input. You only need to verify user input, because this restriction often leads to poor user experience.

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.