Regular expressions are often used to validate string formats such as password length ranges, email formats, landline phone numbers, and mobile phone numbers. However, the performance of regular expressions is low. Therefore, it is better not to use regular expressions when validation is required.

Posted below is a validation class I wrote, which contains some commonly used validation. In these validations, it is possible to use regular expressions without regular expression validation, but in this case, it is possible to use regular expression validation code is also written in the method to do comments.

Note: Remember to add empty string validation.

Ps :Javascript version can be found here.

Regular expressions validation need reference System. Text. RegularExpressions namespace.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
using System.Text.RegularExpressions;// Reference the validation namespace
 
namespace XClassLibrary
{
    public class Validator
    {
        #regionMatching method
        /// <summary>
        ///Verifies that the string matches the rules described by the regular expression
        /// </summary>
        /// <param name="inputStr">String to be verified</param>
        /// <param name="patternStr">Regular expression string</param>
        /// <returns>match</returns>
        public static bool IsMatch(string inputStr, string patternStr)
        {
            return IsMatch(inputStr, patternStr, false.false);
        }
 
        /// <summary>
        ///Verifies that the string matches the rules described by the regular expression
        /// </summary>
        /// <param name="inputStr">String to be verified</param>
        /// <param name="patternStr">Regular expression string</param>
        /// <param name="ifIgnoreCase">Whether the match is case insensitive</param>
        /// <returns>match</returns>
        public static bool IsMatch(string inputStr, string patternStr, bool ifIgnoreCase)
        {
            return IsMatch(inputStr, patternStr, ifIgnoreCase, false);
        }
 
        /// <summary>
        ///Verifies that the string matches the rules described by the regular expression
        /// </summary>
        /// <param name="inputStr">String to be verified</param>
        /// <param name="patternStr">Regular expression string</param>
        /// <param name="ifValidateWhiteSpace">Whether to validate blank strings</param>
        /// <returns>match</returns>
        public static bool IsMatch(string inputStr, string patternStr, bool ifValidateWhiteSpace)
        {
            return IsMatch(inputStr, patternStr, false, ifValidateWhiteSpace);
        }
 
        /// <summary>
        ///Verifies that the string matches the rules described by the regular expression
        /// </summary>
        /// <param name="inputStr">String to be verified</param>
        /// <param name="patternStr">Regular expression string</param>
        /// <param name="ifIgnoreCase">Whether the match is case insensitive</param>
        /// <param name="ifValidateWhiteSpace">Whether to validate blank strings</param>
        /// <returns>match</returns>
        public static bool IsMatch(string inputStr, string patternStr, bool ifIgnoreCase, bool ifValidateWhiteSpace)
        {
            if(! ifValidateWhiteSpace &&string.IsNullOrWhiteSpace(inputStr))
                return false;// If there is no requirement to validate a blank string and the passed string to validate is a blank string, it will not match
            Regex regex = null;
            if (ifIgnoreCase)
                regex = new Regex(patternStr, RegexOptions.IgnoreCase);// Specify case-insensitive matching
            else
                regex = new Regex(patternStr);
            return regex.IsMatch(inputStr);
        }
        #endregion
 
        #regionValidation method
        /// <summary>
        ///Validating numbers (type double)
        ///[Can contain a minus sign and a decimal point]
        /// </summary>
        /// <param name="input">String to be verified</param>
        /// <returns>match</returns>
        public static bool IsNumber(string input)
        {
            //string pattern = @"^-? \d+$|^(-? \d+)(\.\d+)? $";
            //return IsMatch(input, pattern);
            double d = 0;
            if (double.TryParse(input, out d))
                return true;
            else
                return false;
        }
 
        /// <summary>
        ///Validation of an integer
        /// </summary>
        /// <param name="input">String to be verified</param>
        /// <returns>match</returns>
        public static bool IsInteger(string input)
        {
            //string pattern = @"^-? \d+$";
            //return IsMatch(input, pattern);
            int i = 0;
            if (int.TryParse(input, out i))
                return true;
            else
                return false;
        }
 
        /// <summary>
        ///Validates non-negative integers
        /// </summary>
        /// <param name="input">String to be verified</param>
        /// <returns>match</returns>
        public static bool IsIntegerNotNagtive(string input)
        {
            //string pattern = @"^\d+$";
            //return IsMatch(input, pattern);
            int i = - 1;
            if (int.TryParse(input, out i) && i >= 0)
                return true;
            else
                return false;
        }
 
        /// <summary>
        ///Validate positive integers
        /// </summary>
        /// <param name="input">String to be verified</param>
        /// <returns>match</returns>
        public static bool IsIntegerPositive(string input)
        {
            //string pattern = @"^[0-9]*[1-9][0-9]*$";
            //return IsMatch(input, pattern);
            int i = 0;
            if (int.TryParse(input, out i) && i >= 1)
                return true;
            else
                return false;
        }
 
        /// <summary>
        ///Verify the decimal
        /// </summary>
        /// <param name="input">String to be verified</param>
        /// <returns>match</returns>
        public static bool IsDecimal(string input)
        {
            string pattern = @ "^ ([+]? [1-9]\d*\.\d+|-? 0\.\d*[1-9]\d*)$";
            return IsMatch(input, pattern);
        }
 
        /// <summary>
        ///Authentication only contains English letters
        /// </summary>
        /// <param name="input">String to be verified</param>
        /// <returns>match</returns>
        public static bool IsEnglishCharacter(string input)
        {
            string pattern = @"^[A-Za-z]+$";
            return IsMatch(input, pattern);
        }
 
        /// <summary>
        ///Authentication contains only numbers and English letters
        /// </summary>
        /// <param name="input">String to be verified</param>
        /// <returns>match</returns>
        public static bool IsIntegerAndEnglishCharacter(string input)
        {
            string pattern = @"^[0-9A-Za-z]+$";
            return IsMatch(input, pattern);
        }
 
        /// <summary>
        ///Verification only includes Chinese characters
        /// </summary>
        /// <param name="input">String to be verified</param>
        /// <returns>match</returns>
        public static bool IsChineseCharacter(string input)
        {
            string pattern = @"^[\u4e00-\u9fa5]+$";
            return IsMatch(input, pattern);
        }
 
        /// <summary>
        ///Verify number length range (0 in front of number)
        ///[To validate a fixed length, pass in the same two length values]
        /// </summary>
        /// <param name="input">String to be verified</param>
        /// <param name="lengthBegin">Initial value of length range (inclusive)</param>
        /// <param name="lengthEnd">Length range End value (inclusive)</param>
        /// <returns>match</returns>
        public static bool IsIntegerLength(string input, int lengthBegin, int lengthEnd)
        {
            //string pattern = @"^\d{" + lengthBegin + "," + lengthEnd + "}$";
            //return IsMatch(input, pattern);
            if (input.Length >= lengthBegin && input.Length <= lengthEnd)
            {
                int i;
                if (int.TryParse(input, out i))
                    return true;
                else
                    return false;
            }
            else
                return false;
        }
 
        /// <summary>
        ///The validation string contains content
        /// </summary>
        /// <param name="input">String to be verified</param>
        /// <param name="withEnglishCharacter">Whether to contain English letters</param>
        /// <param name="withNumber">Whether or not numbers are included</param>
        /// <param name="withChineseCharacter">Whether Chinese characters are included</param>
        /// <returns>match</returns>
        public static bool IsStringInclude(string input, bool withEnglishCharacter, bool withNumber, bool withChineseCharacter)
        {
            if(! withEnglishCharacter && ! withNumber && ! withChineseCharacter)return false;// If there are no letters, numbers, or Chinese characters, return false
            StringBuilder patternString = new StringBuilder();
            patternString.Append("^ [");
            if (withEnglishCharacter)
                patternString.Append("a-zA-Z");
            if (withNumber)
                patternString.Append("0-9." ");
            if (withChineseCharacter)
                patternString.Append(@"\u4E00-\u9FA5");
            patternString.Append("] + $");
            return IsMatch(input, patternString.ToString());
        }
 
        /// <summary>
        ///Validates the string length range
        ///[To validate a fixed length, pass in the same two length values]
        /// </summary>
        /// <param name="input">String to be verified</param>
        /// <param name="lengthBegin">Initial value of length range (inclusive)</param>
        /// <param name="lengthEnd">Length range End value (inclusive)</param>
        /// <returns>match</returns>
        public static bool IsStringLength(string input, int lengthBegin, int lengthEnd)
        {
            //string pattern = @"^.{" + lengthBegin + "," + lengthEnd + "}$";
            //return IsMatch(input, pattern);
            if (input.Length >= lengthBegin && input.Length <= lengthEnd)
                return true;
            else
                return false;
        }
 
        /// <summary>
        ///Verify string length range (string contains only numbers and/or English letters)
        ///[To validate a fixed length, pass in the same two length values]
        /// </summary>
        /// <param name="input">String to be verified</param>
        /// <param name="lengthBegin">Initial value of length range (inclusive)</param>
        /// <param name="lengthEnd">Length range End value (inclusive)</param>
        /// <returns>match</returns>
        public static bool IsStringLengthOnlyNumberAndEnglishCharacter(string input, int lengthBegin, int lengthEnd)
        {
            string pattern = @"^[0-9a-zA-z]{" + lengthBegin + "," + lengthEnd + "} $";
            return IsMatch(input, pattern);
        }
 
        /// <summary>
        ///Validates the string length range
        ///[To validate a fixed length, pass in the same two length values]
        /// </summary>
        /// <param name="input">String to be verified</param>
        /// <param name="withEnglishCharacter">Whether to contain English letters</param>
        /// <param name="withNumber">Whether or not numbers are included</param>
        /// <param name="withChineseCharacter">Whether Chinese characters are included</param>
        /// <param name="lengthBegin">Initial value of length range (inclusive)</param>
        /// <param name="lengthEnd">Length range End value (inclusive)</param>
        /// <returns>match</returns>
        public static bool IsStringLengthByInclude(string input, bool withEnglishCharacter, bool withNumber, bool withChineseCharacter, int lengthBegin, int lengthEnd)
        {
            if(! withEnglishCharacter && ! withNumber && ! withChineseCharacter)return false;// If there are no letters, numbers, or Chinese characters, return false
            StringBuilder patternString = new StringBuilder();
            patternString.Append("^ [");
            if (withEnglishCharacter)
                patternString.Append("a-zA-Z");
            if (withNumber)
                patternString.Append("0-9." ");
            if (withChineseCharacter)
                patternString.Append(@"\u4E00-\u9FA5");
            patternString.Append("] {" + lengthBegin + "," + lengthEnd + "} $");
            return IsMatch(input, patternString.ToString());
        }
 
        /// <summary>
        ///Verify the length range of string bytes
        ///[To validate a fixed length, pass in the same two length values; each Chinese character is two bytes long]
        /// </summary>
        /// <param name="input">String to be verified</param>
        /// <param name="lengthBegin">Initial value of length range (inclusive)</param>
        /// <param name="lengthEnd">Length range End value (inclusive)</param>
        /// <returns></returns>
        public static bool IsStringByteLength(string input, int lengthBegin, int lengthEnd)
        {
            //int byteLength = Regex.Replace(input, @"[^\x00-\xff]", "ok").Length;
            //if (byteLength >= lengthBegin && byteLength <= lengthEnd)
            / / {
            // return true;
            / /}
            //return false;
            int byteLength = Encoding.Default.GetByteCount(input);
            if (byteLength >= lengthBegin && byteLength <= lengthEnd)
                return true;
            else
                return false;
        }
 
        /// <summary>
        ///Verify the date
        /// </summary>
        /// <param name="input">String to be verified</param>
        /// <returns>match</returns>
        public static bool IsDateTime(string input)
        {
            DateTime dt;
            if (DateTime.TryParse(input, out dt))
                return true;
            else
                return false;
        }
 
        /// <summary>
        ///Verify the landline number
        ///[3 - or 4-digit area codes; they may be enclosed in parentheses; they may be omitted; they may be separated from the local code by a minus sign or space; they may have 3-digit extensions preceded by a plus or minus sign]
        /// </summary>
        /// <param name="input">String to be verified</param>
        /// <returns>match</returns>
        public static bool IsTelePhoneNumber(string input)
        {
            string pattern = @"^(((\(0\d{2}\)|0\d{2})[- ]?) ? \d{8}|((\(0\d{3}\)|0\d{3})[- ]?) ? \d{7})(-\d{3})? $";
            return IsMatch(input, pattern);
        }
 
        /// <summary>
        ///Verify mobile phone number
        ///[Can match "(+86)013325656352", parentheses can be omitted, the + sign can be omitted, (+86) can be omitted, the 0 before the 11-digit mobile phone number can be omitted; the 11-digit mobile phone number second digit can be any of 3, 4, 5, 6, 7, 8, 9]
        /// </summary>
        /// <param name="input">String to be verified</param>
        /// <returns>match</returns>
        public static bool IsMobilePhoneNumber(string input)
        {
            string pattern = @ "^ (\ [(\ +)? 86 \ | ((\ +)? 86)? 0? 1[3456789]\d{9}$";
            return IsMatch(input, pattern);
        }
 
        /// <summary>
        ///Verify phone number (can be a landline or mobile number)
        ///[Landline phone: [3 - or 4-digit area code; enclosed in parentheses; omitted; separated from the local code by a minus sign or space; extension may be 3 digits, preceded by a plus or minus sign]]
        ///[Mobile phone number: [Can match "(+86)013325656352", parentheses can be omitted, + sign can be omitted, (+86) can be omitted, the 0 before the mobile phone number can be omitted; the second digit of the mobile phone number can be 3, 4, 5, 6, 7, 8, 9]]
        /// </summary>
        /// <param name="input">String to be verified</param>
        /// <returns>match</returns>
        public static bool IsPhoneNumber(string input)
        {
            string pattern = @ "^ (\ [(\ +)? 86 \ | ((\ +)? 86)? 0? 1[3456789]\d{9}$|^(((\(0\d{2}\)|0\d{2})[- ]?) ? \d{8}|((\(0\d{3}\)|0\d{3})[- ]?) ? \d{7})(-\d{3})? $";
            return IsMatch(input, pattern);
        }
 
        /// <summary>
        ///Verify zip code
        /// </summary>
        /// <param name="input">String to be verified</param>
        /// <returns>match</returns>
        public static bool IsZipCode(string input)
        {
            //string pattern = @"^\d{6}$";
            //return IsMatch(input, pattern);
            if(input.Length ! =6)
                return false;
            int i;
            if (int.TryParse(input, out i))
                return true;
            else
                return false;
        }
 
        /// <summary>
        ///Verify email
        ///[The @ character can contain only letters, digits, underscores (_), and periods (.); the @ character can contain at least one period and cannot be the last character; the @ character can contain only letters or digits.]
        /// </summary>
        /// <param name="input">String to be verified</param>
        /// <returns>match</returns>
        public static bool IsEmail(string input){mailbox name starts with a number or letter; The email address can consist of letters, digits, periods (.), minus signs (.), and underscores (_). The length of the mailbox name (before the @ character) is3~18A character; Email names cannot end with periods, minus signs, or underscores. Two or more consecutive periods or minus signs cannot appear.//string pattern = @"^[a-zA-Z0-9]((? 
      
            string pattern = @"^([\w-\.]+)@([\w-\.]+)(\.[a-zA-Z0-9]+)$";
            return IsMatch(input, pattern);
        }
 
        /// <summary>
        ///Verify url address (can match IPv4 address but not IPv4 address format verification; IPv6 is not matched.
        ///[Omit "://"; add port number; allow hierarchy; allow parameter transfer; at least one dot in domain name preceded by content]
        /// </summary>
        /// <param name="input">String to be verified</param>
        /// <returns>match</returns>
        public static bool IsURL(string input){Each level of a domain name consists of letters, digits, and a minus sign (the first letter cannot be a minus sign) and is case insensitive. A single domain name cannot exceed the length63, the full length of the domain name does not exceed256A character. In THE DNS system, a full name is a dot. ", for example, www.nit.edu.cn. No last dot represents a relative address. No such as"http://"There is no match for the pass parameter/ / the string pattern = @ "^ ([0-9 a zA - Z] [0-9 a zA - Z -] on conversion {0} \.) + ([0-9 a zA - Z] [0-9 a zA - Z -] on conversion {0}) \.? $";
 
            //string pattern = @"^(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)|(www\.) ) + (([a zA - Z0-9 \. _ -] + \. [a zA - Z] {2, 6}) | ([0-9] {1, 3} \. [0-9] {1, 3} \. [0-9] {1, 3} \. [0-9] {1, 3})) (/ [a - zA - Z0-9 \ & % _ \. / - ~ -] *)? $";
            string pattern = @"^([a-zA-Z]+://)? ([\ w - \] +) (\. [a zA - Z0-9] +) (: \ d {0, 5})? /? ([\w-/]*)\.? ([a-zA-Z]*)\?? (([\w-]*=[\w%]*&?) *) $";
            return IsMatch(input, pattern);
        }
 
        /// <summary>
        ///Verifying an IPv4 Address
        ///[The first and last digits cannot be 0 or 255; 0's complement is allowed]
        /// </summary>
        /// <param name="input">String to be verified</param>
        /// <returns>match</returns>
        public static bool IsIPv4(string input)
        {
            //string pattern = @"^(25[0-4]|2[0-4]\d]|[01]? \d{2}|[1-9])\.(25[0-5]|2[0-4]\d]|[01]? \d? \d)\.(25[0-5]|2[0-4]\d]|[01]? \d? \d)\.(25[0-4]|2[0-4]\d]|[01]? \d{2}|[1-9])$";
            //return IsMatch(input, pattern);
            string[] IPs = input.Split('. ');
            if(IPs.Length ! =4)
                return false;
            int n = - 1;
            for (int i = 0; i < IPs.Length; i++)
            {
                if (i == 0 || i == 3)
                {
                    if (int.TryParse(IPs[i], out n) && n > 0 && n < 255)
                        continue;
                    else
                        return false;
                }
                else
                {
                    if (int.TryParse(IPs[i], out n) && n >= 0 && n <= 255)
                        continue;
                    else
                        return false; }}return true;
        }
 
        /// <summary>
        ///Verifying an IPv6 Address
        ///Can be used to match any valid IPv6 address.
        /// </summary>
        /// <param name="input">String to be verified</param>
        /// <returns>match</returns>
        public static bool IsIPv6(string input)
        {
            string pattern = @ "^ \ s * ((([0-9 a - Fa - f] {1, 4} {7}) ([0-9 a - Fa - f] {1, 4} | :)) | (([0-9 a - Fa - f] {1, 4} {6}) (: [0-9 a - Fa - f] {1, 4} | ((25 [0 to 5] | 2 [0 to 4] \ d \ d \ d | | 1 [ 1-9]? \d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]? \ d {3}))) | | :)) (([0-9 a - Fa - f] {1, 4} {5}) (((: [0-9 a - Fa - f] {1, 4})} {1, 2) | : ((25 [0 to 5] | 2 [0 to 4] \ d \ d \ d | | 1 [1-9]? \d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]? \ d {3}))) | | :)) (([0-9 a - Fa - f] {1, 4} {4}) (((: [0-9 a - Fa - f] {1, 4}, {1, 3}) | ((: [0-9 a - Fa - f] {1, 4})? :((25[0-5]|2[0-4]\d|1\d\d|[1-9]? \d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]? \ d {3})))) | | :)) (([0-9] a - Fa - f {1, 4} {3}) (((: [0-9 a - Fa - f] {1, 4}, {1, 4}) | ((: [0-9 a - Fa - f] {1, 4})} {0, 2: ((25 [0 to 5] | 2 [0 to 4] \ d \ d \ d | | 1 [1-9 ]? \d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]? \ d {3})))) | | :)) (([0-9] a - Fa - f {1, 4} {2}) (((: [0-9 a - Fa - f] {1, 4}, {1, 5}) | ((: [0-9 a - Fa - f] {1, 4}, {0, 3} : ((25 [0 to 5] | 2 [0 to 4] \ d \ d \ d | | 1 [1-9 ]? \d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]? \ d {3})))) | | :)) (([0-9] a - Fa - f {1, 4} {1}) (((: [0-9 a - Fa - f] {1, 4}, {1, 6}) | ((: [0-9 a - Fa - f] {1, 4}, {0, 4} : ((25 [0 to 5] | 2 [0 to 4] \ d \ d \ d | | 1 [1-9 ]? \d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]? \ d {3})))) | | :)) ((((: [0-9 a - Fa - f] {1, 4}, {1, 7}) | ((: [0-9 a - Fa - f] {1, 4}, {0, 5} : ((25 [0 to 5] | 2 [0 to 4] \ d \ d \ d | | 1 [1-9]? \d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]? \d)){3}))|:)))(%.+)? \s*$";
            return IsMatch(input, pattern);
        }
 
        /// <summary>
        ///The address corresponding to the number on the ID card
        /// </summary>
        //enum IDAddress
        / / {
        // Beijing = 11, Tianjin = 12, Hebei = 13, Shanxi = 14, Inner Mongolia = 15, Liaoning = 21, Jilin = 22, Heilongjiang = 23, Shanghai = 31, Jiangsu = 32, Zhejiang = 33,
        // Anhui = 34, Fujian = 35, Jiangxi = 36, Shandong = 37, Henan = 41, Hubei = 42, Hunan = 43, Guangdong = 44, Guangxi = 45, Hainan = 46, Chongqing = 50, Sichuan = 51,
        // Guizhou = 52, Yunnan = 53, Tibet = 54, Shaanxi = 61, Gansu = 62, Qinghai = 63, Ningxia = 64, Xinjiang = 65, Taiwan = 71, Hong Kong = 81, Macao = 82, foreign = 91
        / /}
 
        /// <summary>
        ///Verify first Generation ID Number (15 digits)
        ///[A 15-digit number; matches the address of the corresponding province; birthdays match correctly]
        /// </summary>
        /// <param name="input">String to be verified</param>
        /// <returns>match</returns>
        public static bool IsIDCard15(string input)
        {
            // Verify that it can be converted to a 15-bit integer
            long l = 0;
            if (!long.TryParse(input, outl) || l.ToString().Length ! =15)
            {
                return false;
            }
            // Verify that the provinces match
            // The 1 and 2 digits are the codes of provincial governments, the 3 and 4 digits are the codes of local and municipal governments, and the 5 and 6 digits are the codes of county and district governments.
            string address = "11,12,13,14,15,21,22,23,31,32,33,34,35,36,37,41,42,43,44,45,46,50,51,52,53,54,61,62,63,64,65,71,81,82,91,";
            if(! address.Contains(input.Remove(2) + ","))
            {
                return false;
            }
            // Verify that birthdays match
            string birthdate = input.Substring(6.6).Insert(4."/").Insert(2."/");
            DateTime dt;
            if(! DateTime.TryParse(birthdate,out dt))
            {
                return false;
            }
            return true;
        }
 
        /// <summary>
        ///Verify the second generation ID Number (18-digit, GB11643-1999 standard)
        ///[Length is 18 bits; the first 17 bits are numbers, and the last bit can be case X; matches the address of the corresponding province; the date of birth can be matched correctly; the parity code can be matched correctly]
        /// </summary>
        /// <param name="input">String to be verified</param>
        /// <returns>match</returns>
        public static bool IsIDCard18(string input)
        {
            // Verify that it can be converted to the correct integer
            long l = 0;
            if (!long.TryParse(input.Remove(17), outl) || l.ToString().Length! =17 || !long.TryParse(input.Replace('x'.'0').Replace('X'.'0'), out l))
            {
                return false;
            }
            // Verify that the provinces match
            // The 1 and 2 digits are the codes of provincial governments, the 3 and 4 digits are the codes of local and municipal governments, and the 5 and 6 digits are the codes of county and district governments.
            string address = "11,12,13,14,15,21,22,23,31,32,33,34,35,36,37,41,42,43,44,45,46,50,51,52,53,54,61,62,63,64,65,71,81,82,91,";
            if(! address.Contains(input.Remove(2) + ","))
            {
                return false;
            }
            // Verify that birthdays match
            string birthdate = input.Substring(6.8).Insert(6."/").Insert(4."/");
            DateTime dt;
            if(! DateTime.TryParse(birthdate,out dt))
            {
                return false;
            }
            // Check code verification
            // Check code:
            // (1) the weighted summation formula of the 17-digit ontology code
            //S = Sum(Ai * Wi), i = 0, ... 16. Sum the weights of the first 17 digits
            //Ai: indicates the id card number in position I
            //Wi: indicates the weighting factor at the ith position
            //Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 
            // (2) compute module
            //Y = mod(S, 11) 
            // (3) Obtain the corresponding check code through the module
            //Y: 0 1 2 3 4 5 6 7 8 9 10 
            // Check code: 1 0 X 9 8 7 6 5 4 3 2
            string[] arrVarifyCode = ("1, 0, x, 9,8,7,6,5,4,3,2").Split(', ');
            string[] Wi = ("Seven,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(', ');
            char[] Ai = input.Remove(17).ToCharArray();
            int sum = 0;
            for (int i = 0; i < 17; i++)
            {
                sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
            }
            int y = - 1;
            Math.DivRem(sum, 11.out y);
            if(arrVarifyCode[y] ! = input.Substring(17.1).ToLower())
            {
                return false;
            }
            return true;
        }
 
        /// <summary>
        ///Verify ID number (do not distinguish first and second generation ID number)
        /// </summary>
        /// <param name="input">String to be verified</param>
        /// <returns>match</returns>
        public static bool IsIDCard(string input)
        {
            if (input.Length == 18)
                return IsIDCard18(input);
            else if (input.Length == 15)
                return IsIDCard15(input);
            else
                return false;
        }
 
        /// <summary>
        ///Validation of longitude
        /// </summary>
        /// <param name="input">String to be verified</param>
        /// <returns>match</returns>
        public static bool IsLongitude(string input){range for- 180.~180The decimal number must be1to5position//string pattern = @"^[-\+]? ((1[0-7]\d{1}|0? \ \ d {1, 2}). \ d {1, 5} \ | 180. 0} {1 and 5) $";
            //return IsMatch(input, pattern);
            float lon;
            if (float.TryParse(input, out lon) && lon >= - 180. && lon <= 180)
                return true;
            else
                return false;
        }
 
        /// <summary>
        ///Verify the latitude
        /// </summary>
        /// <param name="input">String to be verified</param>
        /// <returns>match</returns>
        public static bool IsLatitude(string input){range for- 90.~90The decimal number must be1to5position//string pattern = @"^[-\+]? ([0 to 8]? \ d {1} \ \ d {1, 5} 90 \. | 0} {1 and 5) $";
            //return IsMatch(input, pattern);
            float lat;
            if (float.TryParse(input, out lat) && lat >= - 90. && lat <= 90)
                return true;
            else
                return false;
        }
        #endregion}}Copy the code