var Validator = {
VerityLib: {
IsNotEmpty: function (input) {
if (input != '') {
return true;
} else {
return false;
}
},
IsNumber: function (input) {
var regex = /^-?\d+$|^(-?\d+)(\.\d+)?$/;
if (input.match(regex)) {
return true;
} else {
return false;
}
},
IsInteger: function (input) {
var regex = /^-?\d+$/;
if (input.match(regex)) {
return true;
} else {
return false;
}
},
IsIntegerNotNagtive: function (input) {
var regex = /^\d+$/;
if (input.match(regex)) {
return true;
} else {
return false;
}
},
IsIntegerPositive: function (input) {
var regex = /^[0-9]*[1-9][0-9]*$/;
if (input.match(regex)) {
return true;
} else {
return false;
}
},
IsDecimal: function (input) {
var regex = /^([-+]?[1-9]\d*\.\d+|-?0\.\d*[1-9]\d*)$/;
if (input.match(regex)) {
return true;
} else {
return false;
}
},
IsEnglishCharacter: function (input) {
var regex = /^[A-Za-z]+$/;
if (input.match(regex)) {
return true;
} else {
return false;
}
},
IsIntegerAndEnglishCharacter: function (input) {
var regex = /^[0-9A-Za-z]+$/;
if (input.match(regex)) {
return true;
} else {
return false;
}
},
IsChineseCharacter: function (input) {
var regex = /^[\u4e00-\u9fa5]+$/;
if (input.match(regex)) {
return true;
} else {
return false;
}
},
IsIntegerLength: function (input, lengthBegin, lengthEnd) {
var pattern = '^\\d{' + lengthBegin + ',' + lengthEnd + '}$';
var regex = new RegExp(pattern);
if (input.match(regex)) {
return true;
} else {
return false;
}
},
IsStringInclude: function (input, withEnglishCharacter, withNumber, withChineseCharacter) {
if (!Boolean(withEnglishCharacter) && !Boolean(withNumber) && !Boolean(withChineseCharacter)) {
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;
}
},
IsStringLength: function (input, LengthBegin, LengthEnd) {
var pattern = '^.{' + lengthBegin + ',' + lengthEnd + '}$';
var regex = new RegExp(pattern);
if (input.match(regex)) {
return true;
} else {
return false;
}
},
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;
}
},
IsStringLengthByInclude: function (input, withEnglishCharacter, withNumber, withChineseCharacter, lengthBegin, lengthEnd) {
if (!withEnglishCharacter && !withNumber && !withChineseCharacter) {
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;
}
},
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;
}
},
IsDateTime: function (input) {
if (Date.parse(input)) {
return true;
} else {
return false;
}
},
IsTelePhoneNumber: function (input) {
var regex = /^(((0\d2|0\d{2})[- ]?)?\d{8}|((0\d3|0\d{3})[- ]?)?\d{7})(-\d{3})?$/;
if (input.match(regex)) {
return true;
} else {
return false;
}
},
IsMobilePhoneNumber: function (input) {
var regex = /^((\+)?86|((\+)?86)?)0?1[3458]\d{9}$/;
if (input.match(regex)) {
return true;
} else {
return false;
}
},
IsPhoneNumber: function (input) {
var regex = /^((\+)?86|((\+)?86)?)0?1[3458]\d{9}$|^(((0\d2|0\d{2})[- ]?)?\d{8}|((0\d3|0\d{3})[- ]?)?\d{7})(-\d{3})?$/;
if (input.match(regex)) {
return true;
} else {
return false;
}
},
IsZipCode: function (input) {
var regex = /^\d{6}$/;
if (input.match(regex)) {
return true;
} else {
return false;
}
},
IsEmail: function (input) {
var regex = /^([\w-\.]+)@([\w-\.]+)(\.[a-zA-Z0-9]+)$/;
if (input.match(regex)) {
return true;
} else {
return false;
}
},
IsURL: function (input) {
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;
}
},
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;
}
},
IsIPv6: function (input) {
var regex = /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-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-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((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-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-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-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((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-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((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-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,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-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((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}))|:)))(%.+)?\s*$/;
if (input.match(regex)) {
return true;
} else {
return false;
}
},
IsIDCard: function (input) {
input = input.toUpperCase();
if (!(/(^\d{15}$)|(^\d{17}([0-9]|X)$)/i.test(input))) {
return false;
}
var arrCity = { 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 (arrCity[parseInt(input.substr(0, 2))] == null) {
return false;
}
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('19' + 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;
}
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;
},
IsLongitude: function (input) {
var regex = /^[-\+]?((1[0-7]\d{1}|0?\d{1,2})\.\d{1,5}|180\.0{1,5})$/;
if (input.match(regex)) {
return true;
} else {
return false;
}
},
IsLatitude: function (input) {
var regex = /^[-\+]?([0-8]?\d{1}\.\d{1,5}|90\.0{1,5})$/;
if (input.match(regex)) {
return true;
} else {
return false;
}
}
}
}