C # Regular validation Daquan regex.ismatch () Regular expression validation
Need to introduce namespaces using System.Text.RegularExpressions;
#regionVerify that the text box is entered as a number/// <summary> ///validation is not a number (contains integers and decimals)/// </summary> /// <param name= "str" ></param> /// <returns></returns> Public Static BOOLGetnum (stringstr) { returnRegex.IsMatch (str,@"^[-]?\d+[.]? \d*$"); } #endregion #regionVerify that the text box is entered as an integer/// <summary> ///Verify that the text box is entered as an integer/// </summary> /// <param name= "Strnum" >input Character</param> /// <returns>returns a value of type bool</returns> Public Static BOOLValidatenum (stringstrnum) { returnRegex.IsMatch (Strnum,"^[0-9]*$"); } #endregion #regionVerify that the text box is entered as a date/// <summary> ///Judging Date/// </summary> /// <param name= "Date" ></param> /// <returns></returns> Public Static BOOLIsvaliddate (stringDate) { //Verify the YYYY-MM-DD format and basically take the leap year and February into account BOOLBvalid = Regex.IsMatch (Date,@"^ (((( 1[6-9]|[ 2-9]\d) \d{2})-(0?[ 13578]|1[02])-(0?[ 1-9]| [12]\d|3[01]) | (((1[6-9]| [2-9]\d) \d{2})-(0?[ 13456789]|1[012])-(0?[ 1-9]| [12]\d|30)] | (((1[6-9]| [2-9]\d] \d{2}) -0?2-(0?[ 1-9]|1\D|2[0-8]) | (((1[6-9]| [2-9]\d] (0[48]|[ 2468][048]| [13579] [26]) | ((16| [2468] [048]| [3579] [26]) (xx)) -0?2-29- )) $"); return(Bvalid && Date.compareto ("1753-01-01") >=0); //merging the date validation expressions for common year and leap years, we get the final validation date format as YYYY-MM-DD for the regular expression://([0-9]{3}[1-9]|[ 0-9]{2}[1-9][0-9]{1}| //[0-9]{1}[1-9][0-9]{2}| [1-9] [0-9] {3}) -(((0[13578]|1[02))-//(0[1-9]|[ 12][0-9]|3[01]) | ((0[469]|11)-(0[1-9]|[ 12][0-9]|30)) | //(02-(0[1-9]|[ 1][0-9]|2[0-8]))) | (([0-9]{2}) (0[48]|[ 2468][048]| //[13579][26]) | ((0[48]| [2468] [048]| [3579] [26]) (xx)) -02-29) } #endregion #regionVerify that the text box is entered as an e-mail//Verifying e-mail Public Static BOOLIsValidEmail (stringStrin) { returnRegex.IsMatch (Strin,@"^ ([\w-\.] +) @ ((\[[0-9]{1,3}\. [0-9] {1,3}\. [0-9] {1,3}\.) | ([\w-]+\.) +)) ([a-za-z]{2,4}| [0-9] {1,3}) (\]?) $"); } #endregion #regionVerify that the text box is entered as a phone number/// <summary> ///Verify that the text box is entered as a phone number/// </summary> /// <param name= "Strphone" >Input String</param> /// <returns>returns a value of type bool</returns> Public Static BOOLValidatephone (stringStrphone) { returnRegex.IsMatch (Strphone,@"\d{3,4}-\d{7,8}"); } #endregion #regionVerify that the text box is entered as a fax number/// <summary> ///Verify that the text box is entered as a fax number/// </summary> /// <param name= "Strfax" >Input String</param> /// <returns>returns a value of type bool</returns> Public Static BOOLValidatefax (stringStrfax) { returnRegex.IsMatch (Strfax,@"86-\d{2,3}-\d{7,8}"); } #endregion #regionVerify whether the IP//gets the IP string HttpContext.Current.Request.UserHostAddress /// <summary> ///whether it is an IP/// </summary> /// <param name= "IP" ></param> /// <returns></returns> Public Static BOOLIsIP (stringIP) { returnRegex.IsMatch (HttpContext.Current.Request.UserHostAddress,@"^ ((2[0-4]\d|25[0-5]| [01]?\d\d?] \.) {3} (2[0-4]\d|25[0-5]| [01]?\d\d?) $"); } Public Static BOOLIsipsect (stringIP) { returnRegex.IsMatch (HttpContext.Current.Request.UserHostAddress,@"^ ((2[0-4]\d|25[0-5]| [01]?\d\d?] \.) {2} (2[0-4]\d|25[0-5]| [01]?\d\d?| \*) \.) (2[0-4]\d|25[0-5]| [01]?\d\d?| \*) $"); } #endregion #regionVerifies whether a string is a YY-MM-DD string/// <summary> ///determines whether a string is a YY-MM-DD string/// </summary> /// <param name= "str" ></param> /// <returns></returns> Public Static BOOLIsdatestring (stringstr) { returnRegex.IsMatch (str,@"(\d{4})-(\d{1,2})-(\d{1,2} )"); } #endregion
The use of regular expressions in C #