一些正则验证-JS

时间:2025-02-25 15:34:32
Validation = {

    textCount: function(field, counter, maxLimit) {
var message = $(field).val();
if ($(field).val().length > maxLimit)
$(field).val(message.substring(0, maxLimit))
//$(counter).text(maxLimit-field.value.length);
},
refreshValidator: function(img, input) {
$(img).attr('src', $(img).attr('src') + "&r=" + Math.random());
$(input).focus();
},
isUrl: function(s) {
var strRegex =
/^((http(s)?|ftp|telnet|news|rtsp|mms):\/\/)?(((\w(\-*\w)*\.)+[a-zA-Z]{2,4})|(((1\d\d|2([0-4]\d|5[0-5])|[1-9]\d|\d).){3}(1\d\d|2([0-4]\d|5[0-5])|[1-9]\d|\d).?))(:\d{0,5})?(\/+.*)*$/;
return strRegex.test(s);
},
isDecimal: function(d) { var pattern = /^(([1-9]\d{0,12})|0)(\.\d{1,2})?$/; return pattern.test(d); },
isEmail: function(s) {
var pattern = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
return pattern.test(s);
},
isLowEmail: function(s) {
var b, e;
b = s.indexOf("@");
e = s.indexOf(".");
if (b <= 0) return false;
if (e < 0 || e == (s.length - 1)) { return false; }
return true;
},
clearNoNum: function(event, obj) {
event = window.event || event;
if (event.keyCode == 37 | event.keyCode == 39) {
return;
}
obj.value = obj.value.replace(/[^\d.]/g, "");
obj.value = obj.value.replace(/^\./g, "");
obj.value = obj.value.replace(/\.{2,}/g, ".");
obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
},
checkNum: function(obj) {
obj.value = obj.value.replace(/\.$/g, "");
},
isInteger: function(value) {
var integerReg = new RegExp(/^\d+$/);
return integerReg.test(value);
},
isValidateReg: function(value) {
var validateReg = /^([A-Za-z0-9\s\-\_\~\!\@\#\$\%\^\&\*\(\)\|\<\>\?\:\;\"\'\.\[\]\{\}\,\+\`\/\\\=]){8,16}$/;
if (validateReg.test(value)) {
return Biz.Common.Validation.isStrengthReg(value);
}
return false;
},
isStrengthReg: function(value) {
if (value.match(/([a-zA-Z])/) && value.match(/([0-9])/)) {
return true;
}
else if (value.match(/([^a-zA-Z0-9])/) && value.match(/([0-9])/)) {
return true;
}
else if (value.match(/([^a-zA-Z0-9])/) && value.match(/([a-zA-Z])/)) {
return true;
}
return false;
}, isDate: function(strValue) {
var objRegExp = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/ if (!objRegExp.test(strValue))
return false;
else {
var arrayDate = strValue.split(RegExp.$1);
var intDay = parseInt(arrayDate[2], 10);
var intYear = parseInt(arrayDate[0], 10);
var intMonth = parseInt(arrayDate[1], 10);
if (intMonth > 12 || intMonth < 1) {
return false;
}
var arrayLookup = { '1': 31, '3': 31, '4': 30, '5': 31, '6': 30, '7': 31,
'8': 31, '9': 30, '10': 31, '11': 30, '12': 31
}
if (arrayLookup[parseInt(arrayDate[1])] != null) {
if (intDay <= arrayLookup[parseInt(arrayDate[1])] && intDay != 0)
return true;
}
if (intMonth - 2 == 0) {
var booLeapYear = (intYear % 4 == 0 && (intYear % 100 != 0 || intYear % 400 == 0));
if (((booLeapYear && intDay <= 29) || (!booLeapYear && intDay <= 28)) && intDay != 0)
return true;
}
}
return false;
},
isZip: function(value) {
var validateReg = /^[0-9]{6}$/;
return validateReg.test(value);
},
checkSpecialChar: function(value) {
var validateReg = /([~!@#$%^&*\/\\,.\(\)]){6,16}$/;
return validateReg.test(value);
},
CheckSpecialString: function(value) {
var validateReg = /[\u0000-\u0008\u000B\u000C\u000E-\u001F\uD800-\uDFFF\uFFFE\uFFFF]/;
return validateReg.test(value);
},
isTel: function(s) {
var patrn = /^\d{3,4}-\d{7,8}(-\d{3,4})?$/
if (!patrn.exec(s)) return false
return true
}, isMobile: function(value) {
var validateReg = /^1\d{10}$/;
return validateReg.test(value);
},
isIndentifyNo: function(value) {
var validateReg = /^[0-9]{14}[A-Za-z0-9]{1}$/;
var validateReg1 = /^[0-9]{17}[A-Za-z0-9]{1}$/;
return (validateReg.test(value) || validateReg1.test(value));
},
getLength: function(value) {
return value.replace(/[^\x00-\xff]/g, "**").length;
},
isLicence: function(value) {
var validateReg = /^[A-Za-z]{10}[0-9]{10}$/;
return validateReg.test(value);
},
isPersonalCard: function(value) {
var validateReg = /(^\d{15}$)|(^\d{17}(\d|[A-Za-z]{1})$)/;
return validateReg.test(value);
},
isOrganizationCodeCard: function(value) {
var validateReg = /^[A-Za-z0-9]{9}$/;
return validateReg.test(value);
},
isBankAccount: function(value) {
var validateReg = /^[1-9]{1}[0-9]*$/;
return validateReg.test(value);
},
MaxLength: function(field, maxlimit) {
var j = field.value.replace(/[^\x00-\xff]/g, "**").length;
var tempString = field.value;
var tt = "";
if (j > maxlimit) {
for (var i = 0; i < maxlimit; i++) {
if (tt.replace(/[^\x00-\xff]/g, "**").length < maxlimit)
tt = tempString.substr(0, i + 1);
else
break;
}
if (tt.replace(/[^\x00-\xff]/g, "**").length > maxlimit) {
tt = tt.substr(0, tt.length - 1);
field.value = tt;
}
else {
field.value = tt;
}
}
}
};