This article is a mirror article of machine translation, please click here to jump to the original article.

View: 55077|Reply: 3

[Source] C# uses regular expressions to verify phone numbers, mobile phone numbers, ID numbers

[Copy link]
Posted on 12/23/2015 6:12:47 PM | | |
  1. 验证电话号码的主要代码如下:
  2.    public bool IsTelephone(stringstr_telephone)
  3.    {
  4.    returnSystem.Text.RegularExpressions.Regex.IsMatch(str_telephone,@"^(\d{3,4}-)?\d{6,8}$");
  5.    }
  6. 验证手机号码的主要代码如下:
  7. public bool IsHandset(string str_handset)
  8.    {
  9.    returnSystem.Text.RegularExpressions.Regex.IsMatch(str_handset,@"^[1]+[3,5]+\d{9}");
  10.    }
  11. 验证身份证号的主要代码如下:
  12.    public bool IsIDcard(stringstr_idcard)
  13.    {
  14.    returnSystem.Text.RegularExpressions.Regex.IsMatch(str_idcard,@"(^\d{18}$)|(^\d{15}$)");
  15.    }
  16. 验证输入为数字的主要代码如下:
  17.    public bool IsNumber(stringstr_number)
  18.    {
  19.    returnSystem.Text.RegularExpressions.Regex.IsMatch(str_number,@"^[0-9]*$");
  20.    }
  21. 验证邮编的主要代码如下:
  22.    public boolIsPostalcode(string str_postalcode)
  23.    {
  24.    returnSystem.Text.RegularExpressions.Regex.IsMatch(str_postalcode,@"^\d{6}$");
  25.    }
Copy code






Previous:A solution to the problem of Ajax Post commits in Chinese garbled characters in IE in JQuery
Next:Today Christmas Eve, I wish you all a happy Christmas Eve, I won't give you apples
 Landlord| Posted on 12/24/2015 6:15:37 PM |
Some of the above are wrong, and the lower I verified that it is correct!


  1. /// <summary>
  2.         /// 判断登录名是否是邮箱
  3.         /// </summary>
  4.         /// <param name="emailStr"></param>
  5.         /// <returns></returns>
  6.         public static bool IsEmail(string emailStr)
  7.         {
  8.             return Regex.IsMatch(emailStr, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
  9.         }

  10.         /// <summary>
  11.         /// 判断登录名是否是电话号码
  12.         /// </summary>
  13.         /// <param name="str_handset"></param>
  14.         /// <returns></returns>
  15.         public static bool IsHandset(string str_handset)
  16.         {
  17.             return Regex.IsMatch(str_handset, @"^[1]+[3,4,5,6,7,8]+\d{9}");
  18.         }
Copy code


 Landlord| Posted on 7/14/2021 1:25:56 PM |
Production environment usage validation

Posted on 3/8/2022 1:28:44 PM |
Learn to learn
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com