日期:2014-05-16  浏览次数:20370 次

javascript中各种正则表达式验证

/*验证email*/?

?var str = email;??

var expression = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;??

var objExp = new RegExp(expression);??

(objExp.test(str) == true){??

?return true;?

}else???

return false;?????

?

?/* 验证电话号码 */?

var? str = tel ;??

var expression = /(\d{3}-?\d{8}|(\d{4}-)(\d{7})/;??

var objExp = new RegExp(expression);??

if(objExp.test(str) == true){??

return true ;???

}else???

return false;?????

?

/* 验证是否为汉字 */?

?var str = name ;??

var expression = /[\u4E00-\u9FA5/;??

var objExp = new RegExp(expression);??

?if(objExp.test(str) == true){??

return true ;???

}else???

return false;??

?

/* 验证身份证号码 */?

var str = NO;??

var expression = /\d{17}[\d|X]|\d{15}/;??

var objExp = new RegExp(expression);??

if(objExp.test(str) == true){??

return true ;???

}else???

?return false;??

?

?/* 验证网址是否合法 */?

var str = url;??

var expression = /http(s)?:\/\/([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?/;??

var objExp = new RegExp(expression);??

?if(objExp.test(str) == true){??

return true ;???

}else???

? return false;??

?

?/* 限制字符串长度str为指定字符串,digit为指定最大长度 */?

function? checkstr(str,digit)??

{?????? var n = 0 ;??

for(int i = 0;i<str.length;i++)??

{??

var leg = str.charCodeAt(i);??

if(leg>255){??

n+=2;??

}else?

n+=1;??

}??

if(n>digit)???

return true ;????

else????

return false;???

}?