类代码: /************************* * * Class:CheckItem * 2009.08.23 **************************/ //-- Contructor function CheckItem(textboxName,msg,validChar,isRequired){ this.textboxName=textboxName; this.msg=msg; this.validChar=validChar; this.isRequired=isRequired; }
/************************* * * Class:FormChecker * 2009.08.23 **************************/ //-- Contructor function FormChecker(){ this.checkItemArray=new Array; }
//-- Add a check Item to array FormChecker.prototype.addCheckItem=function(textboxName,msg,validChar,isRequired){ var checkItem=new CheckItem(textboxName,msg,validChar,isRequired); this.checkItemArray[this.checkItemArray.length]=checkItem; }
//-- Check text field in the form FormChecker.prototype.checkTextBox=function(checkItem){ var validChar=checkItem.validChar; var isRequired=checkItem.isRequired; var inputValue=$(checkItem.textboxName).value; if(isRequired!="true" && inputValue.length<1){ return true; } else{ var regexStr="^"+validChar+"$"; var regex=new RegExp(regexStr); return regex.test(inputValue); } }
//-- judge the validation of a form FormChecker.prototype.isValid=function(){ for(var i=0;i<this.checkItemArray.length;i++){ var toBeCheckedObj=this.checkItemArray[i]; var checkResult=this.checkTextBox(toBeCheckedObj); if(checkResult==true){ continue; } else{ alert(toBeCheckedObj.msg); $(toBeCheckedObj.textboxName).focus(); return false; } }
return true; }
页面代码:
/***************************************************** * 窗口载入时调用的启动函数 *****************************************************/ var formChecker; window.onload=function(){ // 设置主菜单的当前菜单项 setMainmenuCurrentItem(0); // 设置侧边菜单的当前菜单项 setSidemenuCurrentItemByText("合同起草"); // 取得合同类别的数据 // 填充下拉列表框数据 new TypeListbox("contractTypeFirst","0","contractTypeSecond",""); new TypeListbox("contractTypeSecond",$("contractTypeFirst").value,"contractTypeThird",""); // var today=getToday(); // $("createTime").value=today; // 屏蔽了合同起草时间,故这里也屏蔽。何杨,2011年5月6日9:20:34 // $("maintenanceEndTime").value=today; // 初始化检查项 formChecker=new FormChecker(); formChecker.addCheckItem("contractName","请输入一到一百位的合同名",".{1,100}","true"); formChecker.addCheckItem("contractTypeThird","请点击下拉列表框依次选择合同类别",".{1,100}","true"); formChecker.addCheckItem("contractAmount","请输入数字形式的合同总金额(金额限制百亿内)","([0-9]{1})(\\d{0,9})","true"); formChecker.addCheckItem("vendorName","请点击右边图标选择合同对方名称。",".{1,}","true"); formChecker.addCheckItem("fulfillCycle","请输入数字形式的合同履行期限,以月为单位(最多十位数字)","([1-9]{1})(\\d{0,9})","true"); formChecker.addCheckItem("maintenanceStartTime","请点击右边的按钮选择模板选择维保起始日期","(\\d{4})-(\\d{1,2})-(\\d{1,2})","false"); formChecker.addCheckItem("maintenanceEndTime","请点击右边的按钮选择模板选择维保结束日期","(\\d{4})-(\\d{1,2})-(\\d{1,2})","false"); formChecker.addCheckItem("contractBriefFile","请输入零到五百字的合同所属项目概况或依据的文件",".{1,500}","false"); formChecker.addCheckItem("contractDecision","请输入零到一千字的项目决策过程",".{1,1000}","false"); formChecker.addCheckItem("remark","请输入零到两百五十个字的补充说明",".{1,250}","false"); formChecker.addCheckItem("maintenanceDescription","请输入零到一千个字的维保描述",".{1,1000}","false"); }
/********************************************************************* * 得到表单的检查结果 * 何杨,2011-4-17 14:31:40 *********************************************************************/ function getCheckResult(){ // 进行页面输入检查 if(formChecker.isValid()==false){ return false; } // 维保起始日期 var maintenanceStartTime=$("maintenanceStartTime").value; // 维保结束日期 var maintenanceEndTime=$("maintenanceEndTime").value; // 维保起始日期和维保结束日期做比较 if(isNotEmptyString(maintenanceStartTime)==true && isNotEmptyString(maintenanceEndTime)==true){ if(isReasonable(maintenanceStartTime,maintenanceEndTime)==false){ alert("维保结束日期必须晚于维保起始日期."); $("maintenanceEndTime").focus(); return false; } } return true; }
|