1. 程式人生 > >Jquery常用正則驗證

Jquery常用正則驗證

als datetime date 進制 mic [ ] 其他 位數 rip

常用校驗的正則表達式
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: "您輸入了非法危險字符"
}
};

Jquery常用正則驗證