. NET project some common verification operations _ practical skills

Source: Internet
Author: User

In the project needs to the user input information, as well as some method produces the result to verify, generally in the project uses the JS plug-in or the JS to carry on the related information the verification, but considers from the project security angle, may carry on the system JS injection.

If the information entered by the user in the background is relatively safe to authenticate, when the information validation is illegal, you can throw an exception directly in the program and terminate the operation of the program.

There are several more commonly used validation methods that can reduce development time and error in your project:

1. Judge 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 a pure digit and is generally used for validation of query string parameters:

 <summary>
 ///Checks whether a string is a pure number and is generally used for validation of query string parameters.
 ///</summary>
 ///<param name= "value" > string to be validated. </param>
 ///<returns> is a valid bool value. </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 and is generally used for validation of query string parameters:

 <summary>
 ///Checks whether a string is composed of pure letters and numbers and is generally used for validation of query string parameters.
 ///</summary>
 ///<param name= "value" > string to be validated. </param>
 ///<returns> is a valid bool value. </returns> public
 static bool Isletterornumber (string value)
 {return
  quickvalidate ("^[ a-za-z0-9_]*$ ", value);
 }

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

 <summary>
 ///Judge whether it is a number, including decimals and integers.
 ///</summary>
 ///<param name= "value" > string to be validated. </param>
 ///<returns> is a valid bool value. </returns> public
 static bool Isnumber (string value)
 {return
  quickvalidate ("^" 0| ( [1-9]+[0-9]*)] (. [ 0-9]+)? $ ", value);
 }

5. Quickly verify that a string matches the specified regular expression:

 <summary>
 ///quickly verifies that a string matches a specified regular expression.
 ///</summary>
 ///<param name= "Express" > the contents of regular expressions. </param>
 ///<param name= "value" > string to be validated. </param>
 ///<returns> is a valid bool value. </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 a message:

 <summary>
 ///Determine if a string is a message
 ///</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. Judge whether a string is a ZIP code:

 <summary>
 ///to determine if 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 if a string is in ID format:

 <summary>///Determine if a string is an ID format///</summary>///<param name= "value" ></param>///<ret
  urns></returns> public static bool Isidcard (string value) {System.Text.RegularExpressions.Regex Regex;
  String[] Strarray; if (value. Length!= && (value.
  Length!= 0x12)) {return false; } if (value.
  Length = = {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 ("+ strarray[2]"), Int. Parse (strarray[3]), Int.
   Parse (Strarray[4]));
  return true;
  catch {return false;
  The 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. Judgment is not pure Chinese:

 <summary>
 ///judgment is not 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 if a string is a cell 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 phone number:

 <summary>
 ///Determine whether a string is a phone 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 Web address:

 <summary>
 ///to determine if 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>
 ///Determines 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 is a letter plus a number:

 <summary>
 ///Judge whether a string is a letter plus 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 method adopts the method encapsulation, in the actual project, may encapsulate all methods in the class, the method is defined as the static method, can directly call in the project the verification method, may greatly enhance the project development speed.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.