
对比:
1,以前的页面验证代码
<span style="font-size:18px;">//为了验证就医单号是否为空
var isDBNull = 'true'
//判断就医单号是否为空,为空:true,否则false;
function isInoputNull(){
for(var i = 0;i < detailSourceList.length; i++){
var dateStr = $('#txtOriginalCode' + i)[0].value;
if(dateStr.trim()==''){
isDBNull='false';
}
}
}</span>
这样我们每个需要验证的页面都要在提交时走一遍这个函数,我们如果验证多了,就会在提交表单是写N个类似的函数 ,这样,实际浪费的是我们的时间!
2,改进之后
<span style="font-size:18px;">
<input type="text" vtype="NumAndStr" notNull="true" vdisp="提示信息" id="isOldLeader" value="${LoginUser.NLeader}"/>
</span>
在提交表单是,我们只需要调用一个统一的验证入口(doValidate)就可以了
验证js文件:validateform.js
/**
表单验证类, 可以根据需要,参考下面的代码自行扩充
author: guoerhua@126.com
使用方法:在标签中可以添加以下属性
vtype: 输入域的类型,取值为[LetterStr, NumAndStr, NumStr, ...], 可参考validrule
notNull: 是否为空,取值为[true, false]
vdisp: 表示提示信息
*/
var validrule = new Object();
validrule.LetterStr = /^([a-zA-Z]+)?$/;
validrule.NumAndStr = /^([0-9a-zA-Z]+)?$/;
validrule.NumStr = /^(\d*)?$/;
validrule.string = /^([^'<>]+)?$/;
validrule.int = /^(\d{1,9})?$/;
validrule.minusint = /^(\-([1-9])(\d*))?$/;
validrule.float = /^((\.([0-9]\d*))|(([0-9]\d*)\.\d+$)|([0-9]\d*))?$/;
validrule.date = /^((([1-9]\d{3})|([1-9]\d{1}))-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1]))?$/;
validrule.time = /^((0[1-9]|1[0-9]|2[0-4]):([0-5][0-9]):([0-5][0-9]))?$/;
validrule.datetime = /^((([1-9]\d{3})|([1-9]\d{1}))-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1]) (0[1-9]|1[0-9]|2[0-4]):([0-5][0-9]):([0-5][0-9]))?$/;
validrule.year = /^(\d{4})?$/;
validrule.month = /^([1-9]|0[1-9]|1[0-2])?$/;
validrule.day = /^([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])?$/;
validrule.postcode = /^(\d{6})?$/;
validrule.email = /^(.+\@.+\..+)?$/;
validrule.phone = /^(\(\d{3}\))?(\(?(\d{3}|\d{4}|\d{5})\)?(-?)(\d+))?((-?)(\d+))?$/;
validrule.mobiletel = /^(013(\d{9})|13(\d{9}))?$/;
validrule.ip = /^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5]))?$/;
validrule.idcard = /^(\d{15}|\d{18}|\d{17}X|\d{17}x)?$/;
validrule.tabledefine = /^(([A-Za-z])([A-Za-z0-9|_]){1,18})?$/;
validrule.integer = /^(\d{1,9})?$/;
validrule.number = /^(\d*)?$/;
validrule.url = /^(http:\/\/)/;
validrule.chinese = /^[\u4E00-\u9FA5]*$/;
validrule.notsstring = /^([^'<>\s]+)?$/;
validrule.noEmpty = /^\s*$/; function doValidate( vform )
{
var elems = vform.elements;
var frmLen = elems.length;
var thePat = "";
var strFormatInfo = "";
//对于每一个FROM元素
for(var i=0;i<frmLen;i++)
{
var _elem = elems[i];
if(!_elem)
continue; //为空检查
if(_elem.notNull != null && _elem.notNull == "true")
{
if(validrule.noEmpty.test(_elem.value))
{
alert("请填写"+_elem.vdisp+"!");
_elem.focus();
return false;
}
} if(_elem.tagName=="TEXTAREA"){
if(!validateTextareaMaxlength(_elem)){
return false;
}
} //类型检查
if(_elem.vtype == null)
{
continue;
}
if(_elem.vtype=="none")
{
thePat = "";
strFormatInfo = "";
}
if(_elem.vtype=="LetterStr")
{
thePat = validrule.LetterStr;
strFormatInfo = "纯字母字符串";
}
if(_elem.vtype=="NumAndStr")
{
thePat = validrule.NumAndStr;
strFormatInfo = "数字和字母字符串";
}
if(_elem.vtype=="NumStr")
{
thePat = validrule.NumStr;
strFormatInfo = "纯数字组成的字符串";
}
if(_elem.vtype=="string")
{
thePat = validrule.string;
strFormatInfo = "不能包含特殊符的字符串(英文的'<>)";
}
if(_elem.vtype=="int")
{
thePat = validrule.int;
strFormatInfo = "整数";
}
if(_elem.vtype=="minusint")
{
thePat = validrule.minusint;
strFormatInfo = "负整数,比如-123";
}
if(_elem.vtype=="float")
{
thePat = validrule.float;
strFormatInfo = "实数,比如356.32";
}
if(_elem.vtype=="date")
{
thePat = validrule.date;
strFormatInfo = "日期型,比如 2004-08-12";
}
if(_elem.vtype=="time")
{
thePat = validrule.time;
strFormatInfo = "时间型,比如08:37:29";
}
if(_elem.vtype=="datetime")
{
thePat = validrule.datetime;
strFormatInfo = "日期时间型,比如2004-08-12 08:37:29";
}
if(_elem.vtype=="year")
{
thePat = validrule.year;
strFormatInfo = "年代格式,比如 2005";
}
if(_elem.vtype=="month")
{
thePat = validrule.month;
strFormatInfo = "月份格式,比如 08";
}
if(_elem.vtype=="day")
{
thePat = validrule.day;
strFormatInfo = "日子格式,比如 14";
}
if(_elem.vtype=="postcode")
{
thePat = validrule.postcode;
strFormatInfo = "邮编,比如 100001";
}
if(_elem.vtype=="email")
{
thePat = validrule.email;
strFormatInfo = "电子邮件格式,比如 msm@hotmail.com";
}
if(_elem.vtype=="phone")
{
thePat = validrule.phone;
strFormatInfo = "电话号码格式,比如010-67891234";
}
if(_elem.vtype=="mobiletel")
{
thePat = validrule.mobiletel;
strFormatInfo = "手机号码格式,比如13867891234";
}
if(_elem.vtype=="ip")
{
thePat = validrule.ip;
strFormatInfo = "机器ip地址格式,比如 172.22.169.11";
}
if(_elem.vtype=="url")
{
thePat = validrule.url;
strFormatInfo = "url地址格式,比如 http://www.baidu.cn";
}
if(_elem.vtype=="idcard")
{
thePat = validrule.idcard;
strFormatInfo = "身份证号码,比如15位或者18位数字";
}
if(_elem.vtype=="tabledefine")
{
thePat = validrule.tabledefine;
strFormatInfo = "p_tablename";
}
if(_elem.vtype=="integer")
{
thePat = validrule.integer;
strFormatInfo = "整数";
}
if(_elem.vtype=="number")
{
thePat = validrule.number;
strFormatInfo = "数字字符";
}
if(_elem.vtype=="chinese")
{
thePat = validrule.chinese;
strFormatInfo = "汉字";
}
if(_elem.vtype == "notsstring")
{
thePat = validrule.notsstring;
strFormatInfo = "不包含空格和特殊字符的字符串(英文的'<>)";
} var gotIt = null;
if(thePat!="")
{
gotIt = thePat.exec(_elem.value);
}
if(gotIt == null)
{
alert(_elem.vdisp+"输入不合法,格式应为:"+strFormatInfo);
_elem.focus();
return false;
}
}
return true;
} function validateTextareaMaxlength(_elem){
var strLen = stringLength(_elem.value);
if(_elem.maxlength != null && strLen > _elem.maxlength)
{
var vdisp = _elem.vdisp || "";
alert(vdisp + "超过限定长度,请检查! \r\n\r\n" + vdisp + "限定长度为" + _elem.maxlength + ", 实际长度为" + strLen + ". \r\n(中文字符长度为2)");
_elem.focus();
return false;
}
return true;
} function stringLength(str){
if(str==null) return 0;
var n = 0;
for(var i=0; i<str.length; i++){
if(str.charCodeAt(i)>255)
n = n + 2;
else n++;
}
return n;
}