!--NEWSZW_HZH_BEGIN--
javascript常常使用正则表达式汇总
/**
* 检验各种规矩
* @param str 检验的内容
* @param cType 预设的检验规矩 字符串[
* empty, 检验是不是为空
* telphone, 座机手机号码
* allphone, 所有手机号码
* ydphone, 移动手机号码
* ltphone, 联通手机号码
* dxphone, 电信手机号码
* email, 邮箱
* url, 网址
* cn, 汉字
* image, 图片格局
* emscode, 邮政编码
* isEmpty, 检讨是不是为空
* isint, 整数
* isfloat, 断定是不是为正小数
* isnumber, 断定为实数
* words, 断定是不是为英文字母
* wordsAndNum, 断定是不是为字母+数字
* wordsAndNumAndDownline, 断定是不是由数字、26个英文字母或下划线组成的字符串
* qq, QQ检验
* personCard18, 身份证18位
* personCard15, 身份证15位
* ]
* @param regex 自定义表达式 传入格局例如:"^\-?[1-9]+\d*$"
*
* @description cType 与 regex 只能有一个为空
* 如 checkObjectByRegex("测试中文", "cn"); // 断定中文
* 如 checkObjectByRegex("测试中文", null, "^[\u4e00-\u9fa5]+$"); // 自定义表达式正则
* @return {boolean}
*/
function checkObjectByRegex(str, cType, regex) {
/**
* 定义验证各种格局类型的正则表达式对象
*/
var Regexs = {
telphone: (/^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/), //座机手机号码
allphone: (/^((13[0-9])|(14[57])|(15[0-9])|(17[678])|(18[0-9]))[0-9]{8}$/), //所有手机号码
ydphone: (/^((13[4-9])|(15[012789])|147|178|(18[23478]))[0-9]{8}$/), //移动手机号码
ltphone: (/^((13[0-2])|(145)|(15[56])|(176)|(18[56]))[0-9]{8}$/), //联通手机号码
dxphone: (/^((133)|(153)|(177)|(180)|(181)|(189))[0-9]{8}$/), //电信手机号码
email: (/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/),//邮箱
url: (/(?:https|http|ftp|rtsp|mms):\/\/.+\/[\w]+\.[\w]+/), //网址
cn: (/^[\u4e00-\u9fa5]+$/i), //汉字
image: (/\.jpg$|\.jpeg$|\.png$/i), //图片格局
emscode: (/^[1-9]\d{5}$/), //邮政编码
isint: (/^(\-)?[1-9]+\d*$/), //整数
isfloat: (/^[0-9]+\.?[0-9]*$/), //断定是不是为正小数
isnumber: (/^[-\+]?\d+(\.\d+)?$/), //断定为实数
words: (/^[A-Za-z]+$/), //断定是不是为英文字母
wordsAndNum: (/^[A-Za-z0-9]+$/), //断定是不是为字母+数字
wordsAndNumAndDownline: (/^\w+$/), //断定是不是由数字、26个英文字母或下划线组成的字符串
qq: (/^[1-9]\d{4,11}$/), //QQ
personCard18: (/^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})(\d|X)$/), //身份证18位
personCard15: (/^(\d{6})()?(\d{2})(\d{2})(\d{2})(\d{3})$/) //身份证15位
};
var nReg;
if (str == null || typeof(str) == "undefined") {
str = "";
}
if (cType != null && typeof(cType) != "undefined") {
if (cType == "isEmpty") {
str = $.trim(str);
if (str != null && typeof(str) != "undefined" && str != "") {
return false;
} else return true;
}
nReg = Regexs[cType];
if (str == null || str == "") return false; //输入为空,以为是验证通过
// 针对 18位身份证单独处置
if (cType == "personCard18") {
var ary = str.match(Regexs[cType]);
if (!(parseInt(ary[3]) >= 1900)) return false;
var D = new Date(ary[3] + "/" + ary[4] + "/" + ary[5]);
var isTrue = D.getFullYear() == ary[3] && (D.getMonth() + 1) == ary[4] && D.getDate() == ary[5];
return isTrue;
}
// 针对 15位身份证单独处置
if (cType == "personCard15") {
var ary = str.match(Regexs[cType]);
var D = new Date("19" + ary[3] + "/" + ary[4] + "/" + ary[5]);
var isTrue = D.getYear() == ary[3] && (D.getMonth() + 1) == ary[4] && D.getDate() == ary[5];
return isTrue;
}
} else {
// 自定义正则表达式处置
if (regex != null && typeof(regex) != "undefined") {
nReg = new RegExp(regex);
} else {
return false;
}
}
return nReg.test(str);
}
/**