Some common validation actions in. Net projects

  • 2021-08-16 23:44:18
  • OfStack

In the project, we need to verify the information input by users and the results generated by some methods. Generally, js plug-in or js are used to verify the relevant information in the project, but from the perspective of project security, js injection can be carried out on the system.

If the information input by the user is verified in the background, it will be relatively safe. When the information verification is illegal, you can throw an exception directly in the program to terminate the operation of the program.

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

1. Judge the domain name:


 /// <summary>
 ///  Ordinary 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. 1 is generally used to verify the validity of query string parameters:


 /// <summary>
 ///  Check 1 Whether the string is composed of pure numbers, 1 Used for validation of query string parameters. 
 /// </summary>
 /// <param name="value"> The string to validate. </param>
 /// <returns> Whether it is legal or not 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. 1 is generally used to verify the validity of query string parameters:


 /// <summary>
 ///  Check 1 Whether the strings are composed of pure letters and numbers, 1 Used for validation of query string parameters. 
 /// </summary>
 /// <param name="value"> The string to validate. </param>
 /// <returns> Whether it is legal or not bool Value. </returns>
 public static bool IsLetterOrNumber(string value)
 {
  return QuickValidate("^[a-zA-Z0-9_]*$", value);
 }

4. Determine whether it is a number, including decimal and integer:


 /// <summary>
 ///  Determine whether it is a number, including decimal and integer. 
 /// </summary>
 /// <param name="value"> The string to validate. </param>
 /// <returns> Whether it is legal or not 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>
 ///  Quick validation 1 Whether the string matches the specified regular expression. 
 /// </summary>
 /// <param name="express"> The contents of the regular expression. </param>
 /// <param name="value"> The string to validate. </param>
 /// <returns> Whether it is legal or not 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>
 ///  Judge 1 Whether the 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. Determine whether a string is a zip code:


 /// <summary>
 ///  Judge 1 Whether the 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 ID format:


 /// <summary>
 ///  Judge 1 Whether the string is 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. Judge whether it is pure Chinese:


 /// <summary>
 ///  Judge 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>
 ///  Judge 1 Is the string 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 phone number:


 /// <summary>
 ///  Check 1 Whether the string is composed of pure numbers, 1 Used for validation of query string parameters. 
 /// </summary>
 /// <param name="value"> The string to validate. </param>
 /// <returns> Whether it is legal or not bool Value. </returns>
 public static bool IsNumeric(string value)
 {
  return QuickValidate("^[-]?[1-9]*[0-9]*$", value);
 }
0

12. Determine whether a string is a URL:


 /// <summary>
 ///  Check 1 Whether the string is composed of pure numbers, 1 Used for validation of query string parameters. 
 /// </summary>
 /// <param name="value"> The string to validate. </param>
 /// <returns> Whether it is legal or not bool Value. </returns>
 public static bool IsNumeric(string value)
 {
  return QuickValidate("^[-]?[1-9]*[0-9]*$", value);
 }
1

13. Determine whether a string is an IP address:


 /// <summary>
 ///  Check 1 Whether the string is composed of pure numbers, 1 Used for validation of query string parameters. 
 /// </summary>
 /// <param name="value"> The string to validate. </param>
 /// <returns> Whether it is legal or not bool Value. </returns>
 public static bool IsNumeric(string value)
 {
  return QuickValidate("^[-]?[1-9]*[0-9]*$", value);
 }
2

14. Determine if a string is alphanumeric:


 /// <summary>
 ///  Judge 1 Is the string alphanumeric 
 /// 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;
 }

In the actual project, all the methods can be encapsulated in the class, and the methods are defined as static methods. In the project, the verification methods can be directly called, which can greatly improve the development speed of the project.


Related articles: