Yesterday I wrote validation (C# and regular expressions) and today I wrote a js version of validation. Now post out, in order to facilitate their own access, but also hope to give people in need of help and some inspiration. As I just started to contact JS today, there may be some mistakes and omissions, I hope you can criticize and correct.

var Validator = {
    VerityLib: {
        // Verify that the string is not empty
        IsNotEmpty: function (input) {
            if(input ! =' ') {
                return true;
            } else {
                return false; }},// Verify digits (type double) [can contain a minus sign and a decimal point]
        IsNumber: function (input) {
            var regex = / ^ -? \d+$|^(-? \d+)(\.\d+)? $/;
            if (input.match(regex)) {
                return true;
            } else {
                return false; }},// Validate the integer
        IsInteger: function (input) {
            var regex = / ^ -? \d+$/;
            if (input.match(regex)) {
                return true;
            } else {
                return false; }},// Validate non-negative integers
        IsIntegerNotNagtive: function (input) {
            var regex = /^\d+$/;
            if (input.match(regex)) {
                return true;
            } else {
                return false; }},// Verify positive integers
        IsIntegerPositive: function (input) {
            var regex = / ^ [0-9] * [1-9] [0-9] * $/;
            if (input.match(regex)) {
                return true;
            } else {
                return false; }},// Verify decimals
        IsDecimal: function (input) {
            var regex = / ^ ([+]? [1-9]\d*\.\d+|-? 0\.\d*[1-9]\d*)$/;
            if (input.match(regex)) {
                return true;
            } else {
                return false; }},// The validation contains only English letters
        IsEnglishCharacter: function (input) {
            var regex = /^[A-Za-z]+$/;
            if (input.match(regex)) {
                return true;
            } else {
                return false; }},Validation contains only numbers and English letters
        IsIntegerAndEnglishCharacter: function (input) {
            var regex = /^[0-9A-Za-z]+$/;
            if (input.match(regex)) {
                return true;
            } else {
                return false; }},// Verify only Chinese characters
        IsChineseCharacter: function (input) {
            var regex = /^[\u4e00-\u9fa5]+$/;
            if (input.match(regex)) {
                return true;
            } else {
                return false; }},// Verify the length range of the number (0 in front of the number) [To verify the fixed length, pass in the same two length values]
        IsIntegerLength: function (input, lengthBegin, lengthEnd) {
            var pattern = '^\\d{' + lengthBegin + ', ' + lengthEnd + '} $';
            var regex = new RegExp(pattern);
            if (input.match(regex)) {
                return true;
            } else {
                return false; }},// Verify that the string contains content
        IsStringInclude: function (input, withEnglishCharacter, withNumber, withChineseCharacter) {
            if (!Boolean(withEnglishCharacter) && !Boolean(withNumber) && !Boolean(withChineseCharacter)) {
                return false; // If there are no letters, numbers, or Chinese characters, return false
            }
            var pattern = '^ [';
            if (Boolean(withEnglishCharacter)) {
                pattern += 'a-zA-Z';
            }
            if (Boolean(withNumber)) {
                pattern += '0-9';
            }
            if (Boolean(withChineseCharacter)) {
                pattern += '\\u4E00-\\u9FA5';
            }
            pattern += '] + $';
            var regex = new RegExp(pattern);
            if (input.match(regex)) {
                return true;
            } else {
                return false; }},// Validate string length range [to validate fixed length, pass in the same two length values]
        IsStringLength: function (input, LengthBegin, LengthEnd) {
            var pattern = '^. {' + lengthBegin + ', ' + lengthEnd + '} $';
            var regex = new RegExp(pattern);
            if (input.match(regex)) {
                return true;
            } else {
                return false; }},// Validate string length range (string contains only numbers and/or letters) [To validate fixed length, pass in two values of the same length]
        IsStringLengthOnlyNumberAndEnglishCharacter: function (input, LengthBegin, LengthEnd) {
            var pattern = '^[0-9a-zA-z]{' + lengthBegin + ', ' + lengthEnd + '} $';
            var regex = new RegExp(pattern);
            if (input.match(regex)) {
                return true;
            } else {
                return false; }},// Validate string length range [to validate fixed length, pass in the same two length values]
        IsStringLengthByInclude: function (input, withEnglishCharacter, withNumber, withChineseCharacter, lengthBegin, lengthEnd) {
            if(! withEnglishCharacter && ! withNumber && ! withChineseCharacter) {return false; // If there are no letters, numbers, or Chinese characters, return false
            }
            var pattern = '^ [';
            if (Boolean(withEnglishCharacter))
                pattern += 'a-zA-Z';
            if (Boolean(withNumber))
                pattern += '0-9';
            if (Boolean(withChineseCharacter))
                pattern += '\\u4E00-\\u9FA5';
            pattern += '] {' + lengthBegin + ', ' + lengthEnd + '} $';
            var regex = new RegExp(pattern);
            if (input.match(regex)) {
                return true;
            } else {
                return false; }},[To validate a fixed length, pass in two values of the same length; each Chinese character is two bytes long]
        IsStringByteLength: function (input, lengthBegin, lengthEnd) {
            var regex = /[^\x00-\xff]/g;
            var byteLength = input.replace(regex, 'ok').length;
            if (byteLength >= lengthBegin && byteLength <= lengthEnd) {
                return true;
            } else {
                return false; }},// Verify the date.
        IsDateTime: function (input) {
            if (Date.parse(input)) {
                return true;
            } else {
                return false; }},// Verify the landline number [3 - or 4-digit area code; the area code may be enclosed in parentheses; the area code may be omitted; the area code may be separated from the local code by a minus sign or a space; the extension may be 3-digit, preceded by a plus or minus sign]
        IsTelePhoneNumber: function (input) {
            var regex = /^(((\(0\d{2}\)|0\d{2})[- ]?) ? \d{8}|((\(0\d{3}\)|0\d{3})[- ]?) ? \d{7})(-\d{3})? $/;
            if (input.match(regex)) {
                return true;
            } else {
                return false; }},// Verify mobile phone number [can match "(+86)013325656352", parentheses can be omitted, + sign can be omitted, (+86) can be omitted, the 0 before 11-digit mobile phone number can be omitted; the second digit of 11-digit mobile phone number can be any of 3, 4, 5, 6, 7, 8, 9]
        IsMobilePhoneNumber: function (input) {
            var regex = / ^ (\ [(\ +)? 86 \ | ((\ +)? 86)? 0? 1[3456789]\d{9}$/;
            if (input.match(regex)) {
                return true;
            } else {
                return false; }},// Verify phone number (landline number or mobile number)
        IsPhoneNumber: function (input) {
            var regex = / ^ (\ [(\ +)? 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})? $/;
            if (input.match(regex)) {
                return true;
            } else {
                return false; }},// Verify zip code
        IsZipCode: function (input) {
            var regex = /^\d{6}$/;
            if (input.match(regex)) {
                return true;
            } else {
                return false; }},[The @ character can contain letters, digits, underscores, and periods; the @ character can contain at least one period and the period cannot be the last character; the @ character can contain only letters or digits.]
        IsEmail: function (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.//var regex = /^[a-zA-Z0-9]((? 
      
            var regex = /^([\w-\.]+)@([\w-\.]+)(\.[a-zA-Z0-9]+)$/;
            if (input.match(regex)) {
                return true;
            } else {
                return false; }},// Verify url address (can match IPv4 address but not IPv4 address format verification; [allow omit "://"; add port number; allow hierarchy; allow parameter transfer; at least one dot in domain name preceded by content]
        IsURL: function (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/ / var/regex = ^ ([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}) \.? $/;
 
            //var regex = /^(((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 \ & % _ \. / - ~ -] *)? $/;
            var regex = /^([a-zA-Z]+:\/\/)? ([\ w - \] +) (\. [a zA - Z0-9] +) (: \ d {0, 5})? / /? ([\w-\/]*)\.? ([a-zA-Z]*)\?? (([\w-]*=[\w%]*&?) $/ *);
            if (input.match(regex)) {
                return true;
            } else {
                return false; }},// Verify the IPv4 address [the first and last digits cannot be 0 or 255; 0 complement is allowed]
        IsIPv4: function (input) {
            var regex = /^(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])$/;
            if (input.match(regex)) {
                return true;
            } else {
                return false; }},// Verify IPv6 address [can be used to match any valid IPv6 address]
        IsIPv6: function (input) {
            var regex = / ^ \ 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*$/;
            if (input.match(regex)) {
                return true;
            } else {
                return false; }},// Verify id number [verifiable first or second generation ID card]
        IsIDCard: function (input) {
            input = input.toUpperCase();
            // Verify id card number format [First-generation ID Card number is a 15-digit number; second-generation ID Card number is an 18-digit number or a 17-digit number plus the letter X]
            if(! (/(^\d{15}$)|(^\d{17}([0-9]|X)$)/i.test(input))) {
                return false;
            }
            // Verify the province
            var arrCity = { 11: 'Beijing'.12: 'tianjin'.13: 'hebei'.14: 'the shanxi'.15: Inner Mongolia.21: 'the liaoning'.22: 'jilin'.23: 'Heilongjiang'.31: 'Shanghai'.32: 'jiangsu'.33: 'zhejiang'.34: 'anhui'.35: 'fujian'.36: 'jiangxi'.37: 'shandong'.41: 'henan'.42: 'hubei'.43: 'in hunan province'.44: 'in guangdong'.45: 'the guangxi'.46: 'hainan'.50: 'chongqing'.51: 'sichuan'.52: 'guizhou'.53: 'yunnan'.54: 'Tibet'.61: 'the shaanxi'.62: 'gansu'.63: 'the qinghai'.64: 'the ningxia'.65: 'the xinjiang'.71: 'Taiwan'.81: 'Hong Kong'.82: 'the'.91: 'foreign' };
            if (arrCity[parseInt(input.substr(0.2= =))]null) {
                return false;
            }
            // Verify birth date
            var regBirth, birthSplit, birth;
            var len = input.length;
            if (len == 15) {
                regBirth = new RegExp(/^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/);
                birthSplit = input.match(regBirth);
                birth = new Date('the' + birthSplit[2] + '/' + birthSplit[3] + '/' + birthSplit[4]);
                if(! (birth.getYear() ==Number(birthSplit[2]) && (birth.getMonth() + 1) = =Number(birthSplit[3]) && birth.getDate() == Number(birthSplit[4))) {return false;
                }
                return true;
            }
            else if (len == 18) {
                regBirth = new RegExp(/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/i);
                birthSplit = input.match(regBirth);
                birth = new Date(birthSplit[2] + '/' + birthSplit[3] + '/' + birthSplit[4]);
                if(! (birth.getFullYear() ==Number(birthSplit[2]) && (birth.getMonth() + 1) = =Number(birthSplit[3]) && birth.getDate() == Number(birthSplit[4))) {return false;
                }
                // Verify the verification code
                var valnum;
                var arrInt = new Array(7.9.10.5.8.4.2.1.6.3.7.9.10.5.8.4.2);
                var arrCh = new Array('1'.'0'.'X'.'9'.'8'.'7'.'6'.'5'.'4'.'3'.'2');
                var nTemp = 0, i;
                for (i = 0; i < 17; i++) {
                    nTemp += input.substr(i, 1) * arrInt[i];
                }
                valnum = arrCh[nTemp % 11];
                if(valnum ! = input.substr(17.1)) {
                    return false;
                }
                return true;
            }
            return false;
        },
        // Verify longitude
        IsLongitude: function (input) {
            var regex = / ^ [\ +]? ((1[0-7]\d{1}|0? \ d {1, 2}) \. \ d {1, 5} \ | 180 $/ 0} {1 and 5);
            if (input.match(regex)) {
                return true;
            } else {
                return false; }},// Verify latitude
        IsLatitude: function (input) {
            var regex = / ^ [\ +]? ([0 to 8]? \ d {1} \ \ d {1, 5} | \. 90 $/ 0} {1 and 5);
            if (input.match(regex)) {
                return true;
            } else {
                return false; }}}}Copy the code