Jquery常用正则验证

常用校验的正则表达式
var rulesConfig = {
/**
* str.replace(/^\s+|\s+$/g, '')
解析:
str:要替换的字符串
\s : 表示 space ,空格
+: 一个或多个
^: 开始,^\s,以空格开始
$: 结束,\s$,以空格结束
|:或者
/g:global, 全局
/i 执行对大小写不敏感
/m 执行多行匹配
[abc]查找方括号之间的任何字符
[0-9]查找任何从0至9的数字
(x|y)查找任何以|分隔的选项
\d 查找数字
\s 查找空白字符
\b 匹配单词边界
\uxxxx 查找以十六进制数xxxx规定的Unicode字符
n+ 匹配任何包含至少一个n 的字符串
n* 匹配任何包含零个或多个n的字符串
n? 匹配任何包含零个或一个n的字符串
*/
email: {
validator: function(value){
return /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(value);
},
message: '邮箱格式不对'
},

pass: {
validator: function(value){
return /^[!@#$%^&*a-zA-Z0-9_.]{6,15}$/.test(value);
},
message: '密碼格式不正確'
},

space: {//空格开头或者结尾匹配
validator: function(value){
return /^\s+|\s+$/.test(value);
},
message: '用户名不能以空格开头或者结尾'
},

idcard: { // 验证身份证
validator: function(value) {
return /^\d{15}(\d{2}[A-Za-z0-9])?$/i.test(value);
},
message: '身份证号码格式不正确'
},
minLength: {
validator: function(value, param) {
return value.length >= param[0];
},
message: '请输入至少(2)个字符.'
},
length: {
validator: function(value, param) {
var len = $.trim(value).length;
return len >= param[0] && len <= param[1];
},
message: "输入内容长度必须介于{0}和{1}之间."
},
phone: { // 验证电话号码
validator: function(value) {
return /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value);
},
message: '格式不正确,请使用下面格式:020-88888888'
},
mobile: { // 验证手机号码
validator: function(value) {
return /^(13|15|18)\d{9}$/i.test(value);
},
message: '手机号码格式不正确'
},
currency: { // 验证货币
validator: function(value) {
return /^\d+(\.\d+)?$/i.test(value);
},
message: '货币格式不正确'
},
decimal: {
validator: function(value, param) {
var regStr = "^\\d+(\\.\\d+)?$";
if(param)
regStr = "^\\+?(\\d*\\.\\d{" + param[0] + "})$";
var reg = new RegExp(regStr);
return reg.test(value);
},
message: '输入的数据格式不正确'
},
intOrFloat: { // 验证整数或小数
validator: function(value, param) {
var pattStr = "^\\d+(\\.\\d+)?$";
if(param) {
pattStr = "^\\d+(\\.\\d{0," + param[0] + "})?$";
}
return(new RegExp(pattStr)).test(value);
//如果有参数则验证小数的保留位数,下面是原正则表达式
//return /^\d+(\.\d+)?$/i.test(value);
},
message: '请输入数字,并确保格式正确'
},
integer: { // 验证整数
validator: function(value, param) {
var pattern = /^[+]?[0-9]+\d*$/i;
if(param)
pattern = new RegExp("^[0-9]\d{" + param[0] + "}$");
return pattern.test(value);
},
message: '请输入整数'
},
range: {
validator: function(value, param) {
var v1 = parseFloat(param[0]),
v2 = parseFloat(value),
v3 = parseFloat(param[1]);
if(isNaN(v1) || isNaN(v2) || isNaN(v3)) {
return false;
}
return(v1 <= v2 && v2 <= v3);
},
message: '请输入{0}到{1}之间的数字'
},
qq: { // 验证QQ,从10000开始
validator: function(value) {
return /^[1-9]\d{4,9}$/i.test(value);
},
message: 'QQ号码格式不正确'
},
age: { // 验证年龄
validator: function(value) {
return /^(?:[1-9][0-9]?|1[01][0-9]|120)$/i.test(value);
},
message: '年龄必须是0到120之间的整数'
},
chinese: { // 验证中文
validator: function(value, param) {

var pattern = new RegExp("^[\u4e00-\u9fa5]{" + param[0] + "," + param[1] + "}$");
return pattern.test(value);
//return /^[\Α-\¥]+$/i.test(value);
},
message: '请输入中文'
},
english: { // 验证英语
validator: function(value) {
return /^[A-Za-z]+$/i.test(value);
},
message: '请输入英文'
},
unnormal: { // 验证是否包含空格和非法字符
validator: function(value) {
return /.+/i.test(value);
},
message: '输入值不能为空和包含其他非法字符'
},
username: { // 验证用户名
validator: function(value) {
return /^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){5,19}$/i.test(value);
},
message: '用户名不合法(字母开头,允许6-16字节,允许字母数字下划线)'
},
address: {
validator: function(value) {
var reg = /^[< >]+$/;
return !reg.test(value); //匹配是否含有特殊的字符
},
message: '只能输入包括汉字、字母、数字、符号'
},
faxno: { // 验证传真
validator: function(value) {
// return /^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/i.test(value);
return /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value);
},
message: '传真号码不正确'
},
zip: { // 验证邮政编码
validator: function(value) {
return /^[1-9]\d{5}$/i.test(value);
},
message: '邮政编码格式不正确'
},
ip: { // 验证IP地址
validator: function(value) {
return /d+.d+.d+.d+/i.test(value);
},
message: 'IP地址格式不正确'
},
name: { // 验证姓名,可以是中文或英文
validator: function(value) {
return /^[\Α-\¥]+$/i.test(value) | /^\w+[\w\s]+\w+$/i.test(value);
},
message: '请输入姓名'
},
date: { // 验证姓名,可以是中文或英文
validator: function(value) {
//格式yyyy-MM-dd或yyyy-M-d
return /^(?:(?!0000)[0-9]{4}([-]?)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)([-]?)0?2\2(?:29))$/i.test(value);
},
message: '清输入合适的日期格式'
},
msn: {
validator: function(value) {
return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(value);
},
message: '请输入有效的msn账号(例:abc@hotnail(msn/live).com)'
},
equals: {
validator: function(value, param) {
if($("#" + param[0]).val() != "" && value != "") {
return $("#" + param[0]).val() == value;
} else {
return true;
}
},
message: '两次输入的密码不一致!'
},
compareDate: {
validator: function(value, param) {
return dateCompare($(param[0]).datetimebox('getValue'), value); //注意easyui 时间控制获取值的方式
},
message: '开始日期不能大于结束日期'
},
linkMan: {
validator: function(value, param) {
var pattern = /^[\u4e00-\u9fa5]{2,4}$|^[a-zA-Z]{2,20}$/gi;
return pattern.test(value);
},
message: "请输入2-4个汉字或者20个字母"
},
phoneMobile: { //手机或者固话
validator: function(value, param) {
var pattern = /(^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$)|(^((\(\d{3}\))|(\d{3}\-))?(1[358]\d{9})$)/;
return pattern.test(value);
},
message: "请输入固话或者手机号"
},
postCode: {
validator: function(value, param) {
//var pattern = /^[1-9]\d{5}$/;
var pattern = /^[0-9]\d{5}$/;
return pattern.test(value);
},
message: "请输入邮编"
},
product: {
validator: function(value, param) {
var pattern = new RegExp("^([\u4e00-\u9fa5]|[,]|[,]|[“]|[”]|[\"]|[\"]){" + param[0] + "," + param[1] + "}$");
return pattern.test(value);
},
message: "请输入主要产品"
},
companyCode: {
validator: function(value, param) {
var pattern = new RegExp("[a-zA-Z0-9]{8}-[a-zA-Z0-9]");
return pattern.test(value);
},
message: "请输入组织机构代码证"
},
flEmpty: {
validator: function(value, param) {
var reg = /^[\s ]|[ ]$/gi;
return !reg.yongshiyule178.com test(value);
//return !(/^\s+|\s+$/.test(value));
},
message: "首尾不能有空格"
},
timeDiff: { //时间范围验证
validator: function(value, param) {
//validType:'timeDiff[]'
if(param != undefined && param.length == 2) {
try {
var d1 = null,
curd = new Date(value.replace(/-/g, "/")),
d3 = null;
if(param[0] == 0) { //第一个参数=0 那么必须小于等于第二个参数
d3 = new Date(param[1].replace(/-/g, "/"));
rulesConfig.timeDiff.message = "您选择的时间必须大于等于{0}。";
return(curd <= d3);
} else if(param[1] == 0) { //第二个参数=0 那么必须大于等于第一个参数
d1 = new Date(param[0].replace(/-/g, "/"));
rulesConfig.timeDiff.message = "您选择的时间必须大于等于{0}。";
return(curd www.michenggw.com>= d1);
} else {
d1 = new Date(param[0].replace(/-/g, "/"));
d3 = new Date(param[1].replace(/-/g, "/"));
rulesConfig.timeDiff.message = "您选择的时间必须在{0}和{1}之间。";
return(d1 <= curd <= d3);
}
} catch(e) {
rulesConfig.timeDiff.message = "您选择的时间不正确。";
return false;

}

return false;
}
return true;

/* var d = new Date(value.replace(/-/g, "/"))
var d1 = null;
var d2 = null;
if (param[0] != undefined && param[www.dasheng178.com] != undefined) {//两个都不为空的时候需要在时间之间
d1 = new Date(param[0].replace(/-/g, "/"));
d2 = new Date(param[1].replace(/-/g, "/"));
return (d1 < d1 < d2);
} else if (param[1] != undefined) {//第二个参数不为空,则需要时间小于参数
d2 = new Date(param[1].replace(/-/g, "/"));
return (d < d2);
} else if (param[0] != undefined) {//第一个参数不为空,则需要时间大于参数
d1 = new Date(param[www.gouyiflb.cn].replace(/-/g, "/"));
return (d > d1);
}
return true;*/
},
message: "时间范围选择有误{0}{1}"
},
code: {
validator: function(value, param) {
var reg = new RegExp("<.*?script[^>]*?>.*?(<\/.*?script.*?>)*", "ig");
return !reg.test(value);
},
message: "您输入了非法危险字符"
}
};

猜你喜欢

转载自www.cnblogs.com/qwangxiao/p/10201860.html
今日推荐