电脑技术学习

js一个输入检测类

dn001

类代码:
程序代码
<script language="javascript" type="text/javascript">;
String.prototype.trim2 = function();
{;
return this.replace(/(^s*)|(s*$)/g, "");;
};
function $F(objid){;
return document.getElementById(objid).value;;
};
function $(objid){;
return document.getElementById(objid);;
};
function validator(){;
//;
};
validator.prototype={;
errFlag:0,;
isEmpty:function(strVal){;
if( strVal == "" );
return true;;
else
return false;;
},;


isBlank:function (testVal){
var regVal=/^s*$/;;
return (regVal.test(testVal));
},;;;
chLen:function(strVal){;
strVal=strVal.trim2();;
var cArr = strVal.match(/[^x00-xff]/ig);;
return strVal.length + (cArr == null ? 0 : cArr.length)
},;;;
ckLen:function (testVal,min,max){;;;;
//var testVal=document.getElementById(tarFieldObjId).value;
if(max!=0){;
if ((this.chLen(testVal)<min || this.chLen(testVal)>max)){;
return false;;
};
}else{;

if (this.chLen(testVal)<min ){;
return false;;
};
};;;;
return true;;
},;
chEmail:function (testVal){;
var reg_mail = /^(([A-Za-z0-9-]+_+)|([A-Za-z0-9-]+-+)|([A-Za-z0-9-]+.+)|([A-Za-z0-9-]+++))*[A-Za-z0-9_-]+@((w+-+)|(w+.))*w{1,63}.[a-zA-Z]{2,6}$/;;
return(reg_mail.test(testVal));;
},;
isMobile:function (s){//校验手机号码:必须以数字开头,除数字外,可含有“-”;;;;
var patrn=/^[+]{0,1}(d){1,3}[ ]?([-]?((d)|[ ]){1,12})+$/;;;;;
return (patrn.test(s));;
} ,;;;
isTel:function (s) ////校验普通电话、传真号码:可以“+”开头,除数字外,可含有“-”;
{;;
var patrn=/^[+]{0,1}(d){1,3}[ ]?([-]?((d)|[ ]){1,12})+$/;;;;;
return (patrn.test(s));;
}; ,;;
isIdCard: function(cId);
{;
var pattern;;
if (cId.length==15);
{;
pattern= /^d{15}$/;// http://bizhi.knowsky.com/ 正则表达式,15位且全是数字;
if (pattern.exec(cId)==null);
{;
//alert("15位身份证号码必须为数字!");
return false;;
};
if (!isdate("19"+cId.substring(6,8),cId.substring(8,10),cId.substring(10,12)));
{;
//alert("身份证号码中所含日期不正确");
return false;;
};
};
else if (cId.length==18);
{;
pattern= /^d{17}(d|x|X)$/;//正则表达式,18位且前17位全是数字,最后一位只能数字,x,X;
if (pattern.exec(cId)==null);
{;
//alert("18位身份证号码必须为数字!");
return false;;
};
if (!isdate(cId.substring(6,10),cId.substring(10,12),cId.substring(12,14)));
{;
//alert("身份证号码中所含日期不正确");
return false;;
};

var strJiaoYan; =[; "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"];;
var intQuan =[7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1];;
var intTemp=0;;

for(i = 0; i < cId.length - 1; i++);
intTemp +=; cId.substring(i, i + 1); * intQuan[i];;;;

intTemp %= 11;;
if(cId.substring(cId.length - 1,cId.length).toUpperCase()!=strJiaoYan[intTemp]);
{;
//alert("验证码失败!");
return false;;
};
};
else
{;
//alert("长度必须为15或18!");
return false;;
};

return true;;
};
};
function isdate(intYear,intMonth,intDay);
{;
if(isNaN(intYear)||isNaN(intMonth)||isNaN(intDay));
return false;;
if(intMonth>12||intMonth<1);
return false;;

if ( intDay<1||intDay>31);
return false;;;;

if((intMonth==4||intMonth==6||intMonth==9||intMonth==11)&&(intDay>30));
return false;;

if(intMonth==2);
{;
if(intDay>29);
return false;;
if((((intYear%100==0)&&(intYear%400!=0))||(intYear%4!=0))&&(intDay>28));
return false;;
};
return true;;
};
var c=new validator();

使用方法:


程序代码
val=document.getElementById(objid).value;

c.isEmpty(val);//返回布尔值