Asp.net TextBox can only enter numbers and decimal places

Source: Internet
Author: User

In the custom textbox Control, if only numbers are allowed, consider the following three situations:
Characters entered by normal buttons, including Spanish and Chinese characters
Press the keyboard shortcut key to paste the text, that is, ctrl + v.
Click the mouse operation in the context-related menu to paste the text, that is, the "Paste" operation.
Most of the articles of the same type have considered only 1st cases and ignored 2nd and 3 common operations.
Only decimal places are allowed.

Private sub text=keypress (keyascii as integer)

If keyascii <48 or keyascii> 57 then
If keyascii = 46 then
If text1.text = "" or instr (1, text1.text, ".") <> 0 then
Keyascii = 0
Else
Keyascii = 46
End if
Else
Keyascii = 0
End if
End if

End sub


Add this button

 

Private void button#click (object sender, eventargs e)
{
// Add the code to implement Addition
// Add a judgment Statement by yourself, and set the input value to a number. No value is written here.
Int sum = 0;
Int number1 = int. parse (textbox1.text );
Int number2 = int. parse (textbox2.text );
Sum = number1 + number2;
Textbox3.text = sum. tostring ();
}

Only numbers and decimal places can be entered in textbox.

/// <Summary>
/// Only numbers and decimal places can be entered in textbox
/// Generally called in keypress of textbox
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Public static void textbox_keypress (object sender, keypresseventargs e)
{
If (! (E. keychar> = '0') & (e. keychar <= '9') | e. keychar <= 31 ))
{
If (e. keychar = '.')
{
If (textbox) sender). text. trim (). indexof ('.')>-1)
E. handled = true;
}
Else
E. handled = true;
}
Else
{
If (e. keychar <= 31)
{
E. handled = false;
}
}
}

The pre-processing mode of textbox Control Input, that is, shielding non-numeric keys while inputting characters. In practical applications, the post-processing mode is generally adopted, that is, input post-processing is performed in the exit and validate events of the textbox Control -- verification is performed when the control is left. However, the post-event processing mode has the following shortcomings:
When binding to a data source, inputting non-numeric characters may throw an exception. You need to consider exception capture.
Data needs to be judged and error prompts must be provided.

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.