In C #, only numbers can be entered in the implementation text box.

Source: Internet
Author: User
Code

 Keypress event of the text box:
Write the following code in the keypress event of the text box (such as txtnum) You want to enter:
Private void txtnum_keypress (Object sender, keypresseventargs E)
{
If (! Char. isnumber (E. keychar) & E. keychar! = (Char) keys. Back)
{
E. Handled = true;
}
}

Implementation with method: ^ [0-9] {1,} $ Regular Expression in the textchanged event
RegEx r = new RegEx ("^ [0-9] {1, }$ ");
If (! R. ismatch (textbox1.text ))
{
MessageBox. Show ("enter a number ");
}

Text Box onkeydown event:
Txt1.attributes. Add ("onkeydown", "If (! (Event. keycode> = 48 & event. keycode <= 57) | (event. keycode> = 96 & event. keycode <= 105) & (event. keycode! = 8 )&&

(Event. keycode! = 9) & (event. keycode! = 13) & (event. keycode! = 46) {alert ('Enter the number'); Return false ;}");

Text Box keypress event:
Private void txtnumber_keypress (Object sender, system. Windows. Forms. keypresseventargs E)
{
If (E. keychar> = '0' & E. keychar <= '9 ')
{
E. Handled = false;
Return;
}
E. Handled = true;
}

Restrictions on using regular expressions:
Use regular expressions to restrict text box input in a webpage form:
You can only enter Chinese characters using regular expressions: onkeyup = "value = value. replace (/[^ \ u4e00-\ u9fa5]/g, '')" onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text '). replace (/

[^ \ U4e00-\ u9fa5]/g ,''))"
1. you can only enter the full-width characters: onkeyup = "value = value. replace (/[^ \ uff00-\ Uffff]/g, '')" onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata

('Text'). Replace (/[^ \ uff00-\ Uffff]/g ,''))"
2. use a regular expression to limit that only numbers can be entered: onkeyup = "value = value. replace (/[^ \ D]/g, '')" onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text '). replace (/[^ \ D]/g ,''))"


3. you can only enter numbers and English letters using regular expressions: onkeyup = "value = value. replace (/[\ W]/g, '')" onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text '). replace (/

[^ \ D]/g ,''))"



Text Box amount input: <asp: regularexpressionvalidator id = "regularexpressionvalidator1" runat = "server" controltovalidate = "txtamount"
Display = "NONE" errormessage = "the payment amount can only be a number, up to four decimal places" validationexpression = "^ [0-9] + (\. [0-9] {0, 4 })? $ "> </ASP: regularexpressionvalidator>

 

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.