JavaScript常用验证
初始化类extendString后执行,如:
var test=new extendString();
extendString.isEmail("value");
如嫌麻烦可将extendString改成String后可直接在字符串中引用:
如document.getElementById("testId").value.isEmail()
function extendString(){
}
//去掉字符串中所有空字符
extendString.prototype.trimAll=function(oldString){
var reSpace=/\s/g;
return oldString.replace(reSpace,"");
}
//去掉字符串中左边空字符
extendString.prototype.trimLeft=function(oldString){
var reSpace=/\s*(.*)/;
return oldString.replace(reSpace,"$1")
}
//去掉字符串中右边空字符
extendString.prototype.trimRight=function(oldString){
var reSpace=/(.*?)\s+$/;
return oldString.replace(reSpace,"$1")
}
//去掉字符串中左右两边空字符
extendString.prototype.trim=function(oldString){
var reSpace=/^\s*(.*?)\s*$/;
return oldString.replace(reSpace,"$1")
}
//返回字符串长度,当是双字节时长度为2(例如一个汉字长度返回值为2)
extendString.prototype.len=function(oldString){
var stringLen=oldString.length;
var reGb2312=/[^x00-xff]/g; //[^x00-xff]是排除所有ASC值从0到255的所有字符,也就是排除所有单字节字符,剩下的都是非单字节字符(包括汉字,但不只是汉字)
if(reGb2312.test(oldString)){
stringLen=stringLen+oldString.match(reGb2312).length;
}
return stringLen;
}
//返回字符串长度是否在minLen与maxLen之间
extendString.prototype.between=function(oldString,minLen,maxLen){
if(!minLen){
minLen=0;
}
if(minLen>maxLen){
temp=minLen;
minLen=maxLen;
maxLen=temp;
}
var stringLen=this.len(oldString);
return stringLen>=minLen&&stringLen<=maxLen;
}
//是否国内电话号码,未加严格限制,分机用-分格
extendString.prototype.isTelephone=function(oldString){
var reTelephone=/^(010|02\d|0[3-9]\d{2,3})-?\d{7,8}(-\d{1,5})?$/;
return reTelephone.test(oldString)
}
//是否国内手机,可加零
extendString.prototype.isMobilephone=function(oldString){
var reMobilephone=/^0?1(3\d{9}|5(8|9)\d{8})$/;
return reMobilephone.test(oldString)
}
//是否email,前面包括范围(数字、字母、-、.)
extendString.prototype.isEmail=function(oldString){
var reEmail=/^[a-zA-Z0-9]+[a-zA-Z0-9_\.]{3,17}@[a-zA-Z0-9][a-zA-Z0-9\-]*\.[a-zA-Z]{2,4}\.?[a-zA-Z]+$/
return reEmail.test(oldString)
}
}
//去掉字符串中所有空字符
extendString.prototype.trimAll=function(oldString){
var reSpace=/\s/g;
return oldString.replace(reSpace,"");
}
//去掉字符串中左边空字符
extendString.prototype.trimLeft=function(oldString){
var reSpace=/\s*(.*)/;
return oldString.replace(reSpace,"$1")
}
//去掉字符串中右边空字符
extendString.prototype.trimRight=function(oldString){
var reSpace=/(.*?)\s+$/;
return oldString.replace(reSpace,"$1")
}
//去掉字符串中左右两边空字符
extendString.prototype.trim=function(oldString){
var reSpace=/^\s*(.*?)\s*$/;
return oldString.replace(reSpace,"$1")
}
//返回字符串长度,当是双字节时长度为2(例如一个汉字长度返回值为2)
extendString.prototype.len=function(oldString){
var stringLen=oldString.length;
var reGb2312=/[^x00-xff]/g; //[^x00-xff]是排除所有ASC值从0到255的所有字符,也就是排除所有单字节字符,剩下的都是非单字节字符(包括汉字,但不只是汉字)
if(reGb2312.test(oldString)){
stringLen=stringLen+oldString.match(reGb2312).length;
}
return stringLen;
}
//返回字符串长度是否在minLen与maxLen之间
extendString.prototype.between=function(oldString,minLen,maxLen){
if(!minLen){
minLen=0;
}
if(minLen>maxLen){
temp=minLen;
minLen=maxLen;
maxLen=temp;
}
var stringLen=this.len(oldString);
return stringLen>=minLen&&stringLen<=maxLen;
}
//是否国内电话号码,未加严格限制,分机用-分格
extendString.prototype.isTelephone=function(oldString){
var reTelephone=/^(010|02\d|0[3-9]\d{2,3})-?\d{7,8}(-\d{1,5})?$/;
return reTelephone.test(oldString)
}
//是否国内手机,可加零
extendString.prototype.isMobilephone=function(oldString){
var reMobilephone=/^0?1(3\d{9}|5(8|9)\d{8})$/;
return reMobilephone.test(oldString)
}
//是否email,前面包括范围(数字、字母、-、.)
extendString.prototype.isEmail=function(oldString){
var reEmail=/^[a-zA-Z0-9]+[a-zA-Z0-9_\.]{3,17}@[a-zA-Z0-9][a-zA-Z0-9\-]*\.[a-zA-Z]{2,4}\.?[a-zA-Z]+$/
return reEmail.test(oldString)
}