自用备份,若参考,不负责其正确性!
字符验证:
1.验证汉字
\u4e00-\u9fa5]{0,}$
2.英文和数字
^[A-Za-z0-9]+$ 或 ^[A-Za-z0-9]{4,40}$
用法实例:
1.正则提取数字
var str="/Date(1454487480434)/";
var reg=new RegExp(/[^0-9]$/);
console.log(str.replace(/\D/g,""));//结果是1454487480434
2.将字符串格式化成一个数组
var str="[{a:1,aa:11},{b:2},{c:3}]";
var str1=JSON.stringify(str).slice(2,-2).split(/,(?={)/);
console.log(str1);
3.验证输入不能全是空格
var regNull=new RegExp(/^\s$/g);
4.验证邮箱
var emailReg=new RegExp(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/);
if (!emailReg.test(Email)) {
console.log("不符合要求");
}
5.验证手机号
function checkMobile(s){
var regu =/^[1][3][0-9]{9}$/;
var re = new RegExp(regu);
if (re.test(s)) {
return true;
}else{
return false;
}
}
6.检查身份证号
function checkCard(str) {
//15位数身份证正则表达式
var arg1 = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/;
//18位数身份证正则表达式
var arg2 = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[A-Z])$/;
if (str.match(arg1) == null && str.match(arg2) == null) {
return false;
}
else {
return true;
}
}
7.检查特殊字符
function checkQuote(str) {
var items = new Array("~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "{", "}", "[", "]", "(", ")");
items.push(":", ";", "'", "|", "\\", "<", ">", "?", "/", "<<", ">>", "||", "//");
items.push("admin", "administrators", "administrator", "管理员", "系统管理员");
items.push("select", "delete", "update", "insert", "create", "drop", "alter", "trancate");
str = str.toLowerCase();
for (var i = 0; i < items.length; i++) {
if (str.indexOf(items[i]) >= 0) {
return true;
}
}
return false;
}
8.检查url地址是否正确
function checkURL(str) {
if (str.match(/(http[s]?|ftp):\/\/[^\/\.]+?\..+\w$/i) == null) {
return false
}
else {
return true;
}
}
9.检查电话号码
function checkPhone(strPhone) {
var phoneRegWithArea = /^[0][1-9]{2,3}-[0-9]{5,10}$/;
var phoneRegNoArea = /^[1-9]{1}[0-9]{5,8}$/;
//var prompt = "您输入的电话号码不正确!"
if (strPhone.length > 9) {
if (phoneRegWithArea.test(strPhone)) {
return true;
} else {
//alert( prompt );
return false;
}
} else {
if (phoneRegNoArea.test(strPhone)) {
return true;
} else {
//alert(prompt);
return false;
}
}
}
10.校验IP地址
function isIP(strIP) {
if (isNull(strIP)) return false;
var re=/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/g //匹配IP地址的正则表达式
if(re.test(strIP))
{
if( RegExp.$1 <256 && RegExp.$2<256 && RegExp.$3<256 && RegExp.$4<256) return true;
}
return false;
}
11.检查输入对象的值是否符合端口号格式
function isPort( str ){
return (isNumber(str) && str<65536);
}
12.检查输入字符串是否只由英文字母和数字和下划线组成
function isNumberOr_Letter( s ){//判断是否是数字或字母
var regu = "^[0-9a-zA-Z\_]+$";
var re = new RegExp(regu);
if (re.test(s)) {
return true;
}else{
return false;
}
}
13.判断是否是日期
function isDate( date, fmt ) {
if (fmt==null) fmt="yyyyMMdd";
var yIndex = fmt.indexOf("yyyy");
if(yIndex==-1) return false;
var year = date.substring(yIndex,yIndex+4);
var mIndex = fmt.indexOf("MM");
if(mIndex==-1) return false;
var month = date.substring(mIndex,mIndex+2);
var dIndex = fmt.indexOf("dd");
if(dIndex==-1) return false;
var day = date.substring(dIndex,dIndex+2);
if(!isNumber(year)||year>"2100" || year< "1900") return false;
if(!isNumber(month)||month>"12" || month< "01") return false;
if(day>getMaxDay(year,month) || day< "01") return false;
return true;
}
function getMaxDay(year,month) {
if(month==4||month==6||month==9||month==11)
return "30";
if(month==2)
if(year%4==0&&year%100!=0 || year%400==0)
return "29";
else
return "28";
return "31";
}