Can take the form of Web Forms validation, using the TextChanged event of the textbox, whenever the contents of the TextBox changes, call the method validation of the regular expression, with a label after the text prompt input error, the specific code is as follows:
Private voidtextBox1_TextChanged (Objectsender, EventArgs e) { if(TextBox1.Text.Trim ()! ="") { if(! Validate (TextBox1.Text.Trim (),@"^ (-?\d+) (\.\d+)? $") {Label1. Text="Please enter a number"; } Else{Label1. Text="match correct"; } } Else{Label1. Text=""; } } /**////whether the specified regular expression is met Static Public BOOLValidate (stringStrstringregexstr) {Regex regex=NewRegex (REGEXSTR); Match Match=regex. Match (str); if(match. Success)return true; Else return false; }
Note To reference the namespaces: using System.Text.RegularExpressions;
How can I enter only numbers in a TextBox in WinForm (with decimal points)