Some common verification operations in the DotNet project, dotnet project Verification

Source: Internet
Author: User

Some common verification operations in the DotNet project, dotnet project Verification

In the project, you need to verify the information entered by the user and the results generated by some methods. Generally, js plug-ins or js are used in the project to verify the information, but from the perspective of project security, javascript injection can be performed on the system.

If the information entered by the user is verified in the background, it will be relatively safe. If the information verification is invalid, you can directly throw an exception in the program to terminate the program.

Several common verification methods are provided to reduce the development time and error in the project:

1. Determine the Domain Name:

/// <Summary> /// common domain name /// </summary> /// <param name = "value"> </param> /// <returns> </returns> public static bool IsCommonDomain (string value) {return QuickValidate ("^ (www .)? (\ W + \.) {1, 3} (org | org.cn | gov.cn | com | cn | net | cc) $ ", value. ToLower ());}

2. check whether a string is composed of pure numbers. It is generally used to check the validity of string parameters:

/// <Summary> /// check whether a string is composed of pure numbers. It is generally used to query the validity verification of string parameters. /// </Summary> /// <param name = "value"> string to be verified. </Param> // <returns> whether the bool value is valid. </Returns> public static bool IsNumeric (string value) {return QuickValidate ("^ [-]? [1-9] * [0-9] * $ ", value );}

3. check whether a string is composed of pure letters and numbers. It is generally used to query the validity verification of string parameters:

/// <Summary> /// check whether a string is composed of pure letters and numbers. It is generally used to check the validity of string parameters. /// </Summary> /// <param name = "value"> string to be verified. </Param> // <returns> whether the bool value is valid. </Returns> public static bool IsLetterOrNumber (string value) {return QuickValidate ("^ [a-zA-Z0-9 _] * $", value );}

4. determine whether it is a number, including decimals and integers:

/// <Summary> /// determines whether it is a number, including decimals and integers. /// </Summary> /// <param name = "value"> string to be verified. </Param> // <returns> whether the bool value is valid. </Returns> public static bool IsNumber (string value) {return QuickValidate ("^ (0 | ([1-9] + [0-9] *) (. [0-9] + )? $ ", Value );}

5. quickly verify whether a string meets the specified regular expression:

/// <Summary> /// quickly verify whether a string conforms to the specified regular expression. /// </Summary> /// <param name = "express"> content of the regular expression. </Param> /// <param name = "value"> the string to be verified. </Param> // <returns> whether the bool value is valid. </Returns> public static bool QuickValidate (string express, string value) {var myRegex = new System. Text. RegularExpressions. Regex (express); return value. Length! = 0 & myRegex. IsMatch (value );}

6. Determine whether a string is an email:

/// <Summary> /// determine whether a string is an email. /// </summary> /// <param name = "value"> </param> /// <returns> </returns> public static bool IsEmail (string value) {var regex = new System. text. regularExpressions. regex (@ "^ \ w + ([-+.] \ w +) * @ (\ w + ([-.] \ w + )*\.) + ([a-zA-Z] +) + $ ", RegexOptions. ignoreCase); return regex. match (value ). success ;}

7. Determine whether a string is a zip code:

/// <Summary> /// determine whether a string is a zip code. /// </summary> /// <param name = "value"> </param> /// <returns> </returns> public static bool IsZipCode (string value) {return QuickValidate ("^ ([0-9] {6}) $", value );}

8. Determine whether a string is in the ID format:

/// <Summary> /// determine whether a string is in the ID format. /// </summary> /// <param name = "value"> </param> // /<returns> </returns> public static bool IsIdCard (string value) {System. text. regularExpressions. regex regex; string [] strArray; if (value. length! = 15) & (value. Length! = 0x12) {return false;} if (value. length = 15) {regex = new System. text. regularExpressions. regex (@ "^ (\ d {6}) (\ d {2}) (\ d {2}) (\ d {2}) (\ d {3 }) $ "); if (! Regex. match (value ). success) {return false;} strArray = regex. split (value); try {var dateTime = new DateTime (int. parse ("19" + strArray [2]), int. parse (strArray [3]), int. parse (strArray [4]); return true;} catch {return false;} regex = new System. text. regularExpressions. regex (@ "^ (\ d {6}) (\ d {4}) (\ d {2}) (\ d {2}) (\ d {3 }) ([0-9Xx]) $ "); if (! Regex. match (value ). success) {return false;} strArray = regex. split (value); try {var dateTime = new DateTime (int. parse (strArray [2]), int. parse (strArray [3]), int. parse (strArray [4]); return true ;}catch {return false ;}}

9. Determine whether it is pure Chinese:

/// <Summary> /// determine whether it is pure Chinese. /// </summary> /// <param name = "value"> </param> /// <returns> </returns> public static bool IsChinese (string value) {var regex = new System. text. regularExpressions. regex (@ "^ [\ u4E00-\ u9FA5 \ uF900-\ uFA2D] + $", RegexOptions. ignoreCase); return regex. match (value ). success ;}

10. Determine whether a string is a mobile phone number:

/// <Summary> /// determine whether a string is a mobile phone number. /// </summary> /// <param name = "value"> </param> // /<returns> </returns> public static bool IsMobileNum (string value) {var regex = new System. text. regularExpressions. regex (@ "^ (13 | 15) \ d {9} $", RegexOptions. ignoreCase); return regex. match (value ). success ;}

11. Determine whether a string is a telephone number:

/// <Summary> /// determine whether a string is a telephone number. /// </summary> /// <param name = "value"> </param> // /<returns> </returns> public static bool IsPhoneNum (string value) {var regex = new System. text. regularExpressions. regex (@ "^ (86 )? (-)? (0 \ d {2, 3 })? (-)? (\ D {7, 8 })(-)? (\ D {3, 5 })? $ ", RegexOptions. IgnoreCase); return regex. Match (value). Success ;}

12. Determine whether a string is a URL:

/// <Summary> /// determine whether a string is a URL // </summary> /// <param name = "value"> </param> /// <returns> </returns> public static bool IsUrl (string value) {var regex = new System. text. regularExpressions. regex (@ "(http ://)? ([\ W-] + \.) * [\ w-] + (/[\ w -./? % & =] *)? ", RegexOptions. IgnoreCase); return regex. Match (value). Success ;}

13. Determine whether a string is an IP Address:

/// <Summary> /// determine whether a string is an IP address // </summary> /// <param name = "value"> </param> /// <returns> </returns> public static bool IsIp (string value) {var regex = new System. text. regularExpressions. regex (@ "^ (2 [0-4] {1} [0-9] {1}) | (25 [0-5] {1 })) | (1 [0-9] {2}) | ([1-9] {1} [0-9] {1 }) | ([0-9] {1 })). (2 [0-4] {1} [0-9] {1}) | (25 [0-5] {1 })) | (1 [0-9] {2}) | ([1-9] {1} [0-9] {1 }) | ([0-9] {1 })). (2 [0-4] {1} [0-9] {1}) | (25 [0-5] {1 })) | (1 [0-9] {2}) | ([1-9] {1} [0-9] {1 }) | ([0-9] {1 })). (2 [0-4] {1} [0-9] {1}) | (25 [0-5] {1 })) | (1 [0-9] {2}) | ([1-9] {1} [0-9] {1 }) | ([0-9] {1}) $ ", RegexOptions. ignoreCase); return regex. match (value ). success ;}

14. Determine whether a string contains letters and numbers:

/// <Summary> /// determine whether a string is a letter with a number // Regex ("[a-zA-Z0-9]? "/// </Summary> /// <param name =" value "> </param> /// <returns> </returns> public static bool IsWordAndNum (string value) {var regex = new System. text. regularExpressions. regex ("[a-zA-Z0-9]? "); Return regex. Match (value). Success ;}

The above verification methods are encapsulated by methods. In actual projects, all methods can be encapsulated in classes, and the methods are defined as static methods, you can directly call the verification method in the project to greatly improve the development speed of the project.

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.