REM ===============用于表单验证和处理================= REM === (版本1.3 最后更新时间:20:12 2007-1-9 中文文版本) === REM === 编写人OneTwoFree === REM === 不使用mmalert === REM ================================================= Const WebNote_WebForm_ResetStyle="background-color:;color:" Const WebNote_WebForm_AlertStyle="background-color:#FF0000;color:#FFFFFF" REM =============表单验证================ Function StrCanNull(byRef FormChild,AlertText,LimitedLength) FormChild.Value=trim(FormChild.Value) if Len(FormChild.Value)>LimitedLength then FormChild.focus::FormChild.style.cssText=WebNote_WebForm_AlertStyle Call Alert("对不起,您填写的["&AlertText&"]字段长度不符合要求!超过规定的"&LimitedLength&"个中文字符长度了。"&vbcrlf&"请精简该字段后进行提交!") FormChild.style.cssText=WebNote_WebForm_ResetStyle StrCanNull=False else StrCanNull=True end if End Function Function StrNotNull(byRef FormChild,AlertText,LimitedLength) FormChild.Value=trim(FormChild.Value) if FormChild.Value="" then FormChild.focus::FormChild.style.cssText=WebNote_WebForm_AlertStyle Call Alert("["&AlertText&"]字段不能为空!") FormChild.style.cssText=WebNote_WebForm_ResetStyle StrNotNull=False elseif Len(FormChild.Value)>LimitedLength then FormChild.focus::FormChild.ScrollIntoView::FormChild.style.cssText=WebNote_WebForm_AlertStyle Call Alert("对不起,您填写的["&AlertText&"]字段长度不符合要求!超过规定的"&LimitedLength&"个中文字符长度了。"&vbcrlf&"请精简该字段后进行提交!") FormChild.style.cssText=WebNote_WebForm_ResetStyle StrNotNull=False else StrNotNull=True end if End Function Function RadioCheck(byRef FormChild,AlertText) dim it,isRadioChecked isRadioChecked=False for each it in FormChild if it.checked then isRadioChecked=True exit for end if next if isRadioChecked then RadioCheck=True else RadioCheck=False FormChild(0).ScrollIntoView Call Alert(AlertText) end if End Function Function SelectCheck(byRef FormChild,AlertText) if Trim(FormChild.value)="" then SelectCheck=False FormChild.style.cssText=WebNote_WebForm_AlertStyle FormChild.ScrollIntoView Call Alert(AlertText) FormChild.style.cssText=WebNote_WebForm_ResetStyle else SelectCheck=True end if End Function Function NumCanNull(byRef FormChild,AlertText) FormChild.Value=trim(FormChild.Value) if FormChild.Value="" then NumCanNull=True else NumCanNull=NumNotNull(FormChild,AlertText) end if End Function Function NumNotNull(byRef FormChild,AlertText) FormChild.Value=trim(FormChild.Value) if Not IsNumeric(FormChild.Value) then FormChild.style.cssText=WebNote_WebForm_AlertStyle FormChild.focus Call Alert("对不起,您填写的["&AlertText&"]字段的应该为数字!") FormChild.style.cssText=WebNote_WebForm_ResetStyle NumNotNull=False else NumNotNull=True end if End Function Sub mmSubmit(FormObj) FormObj.BtnSubmit.disabled=True FormObj.BtnSubmit.Value="正在保存" FormObj.Submit() End Sub REM =============表单验证================ REM =============表单处理================ Function LoadXML2Obj(XMLPath) dim xmlT Set xmlT=CreateObject("Microsoft.XMLDOM") xmlT.async = false xmlT.load(XMLPath) Set LoadXML2Obj=xmlT.documentElement Set xmlT=Nothing End Function Sub LoadXML_to_Select(XMLPath,objID,value,text) '将xml的数据读到select中 dim objSelect,i Set objSelect=Document.getElementByID(objID) dim tempObj,oOption,root Set root=LoadXML2Obj(XMLPath) Set tempObj=root.childNodes For i=0 to tempObj.Length-1 Set oOption=document.createElement("OPTION") oOption.text=tempObj(i).getAttribute(text) oOption.value=tempObj(i).getAttribute(value) objSelect.add(oOption) Set oOption=Nothing Next Set tempObj=Nothing Set objSelect=Nothing set root=Nothing End Sub Function LoadXML_byFieldName(XMLPath,sFieldName,sFocusValue,sReturnFieldName) REM 读取XMLPath文件,根据sFieldName和sFocusValue,匹配记录返回属性名为sReturnFieldName的值 dim tempObj,oOption,root Set root=LoadXML2Obj(XMLPath) Set tempObj=root.childNodes For i=0 to tempObj.Length-1 if Lcase(tempObj(i).getAttribute(sFieldName))=Lcase(sFocusValue) then LoadXML_byFieldName=tempObj(i).getAttribute(sReturnFieldName) Exit For end if Next if tempObj.Length=0 then LoadXML_byFieldName="" end if Set tempObj=Nothing Set objSelect=Nothing set root=Nothing End Function Sub XML2FormChildNodes(sFormName,sXMLPath) REM 将XML文件中的数据自动填充到名为sFormName的表单的子结点元素中 on error resume next dim sType,Root,it 'window.open sXMLPath,"a" Set Root=LoadXML2Obj(sXMLPath) Set Root=Root.ChildNodes(0) For Each it in Document.Forms(sFormName) sType=Lcase(it.Type) sName=Lcase(it.Name) sTagName=Lcase(it.TagName) if sType="text" then it.Value=Root.SelectSingleNode(sName).Text end if if sType="radio" then Call Focus_Radio(sName,Root.SelectSingleNode(sName).Text) end if if sType="checkbox" then Call Focus_ChekcBox(it,Root.SelectSingleNode(sName).Text) end if if sTagName="select" then Call Focus_Select(sName,Root.SelectSingleNode(sName).Text) end if if sTagName="textarea" then it.Value=Root.SelectSingleNode(sName).Text end if Next End Sub Sub Add_Option(objName,value,Text) dim oOption,objSelect Set objSelect=Document.getElementByID(objName) Set oOption=document.createElement("OPTION") oOption.text=Text oOption.value=value objSelect.add(oOption) Set oOption=Nothing Set objSelect=Nothing End Sub Sub Clear_objSelect(objID) '清空select对象中的children dim objSelect Set objSelect=Document.getElementByID(objID) For Each it in objSelect.options it.innerHTML="" it.removeNode Next End Sub Sub Remove_Option(objName,value) '根据value,默认定位option dim objselect value=Trim(value) if value="" then Exit Sub value=Lcase(value) Set objselect=document.getElementbyID(objName) For Each it in objselect.Options if Lcase(Trim(it.value))=value then it.OuterHTML="" it.RemoveNode Exit Sub end if Next Set objselect=Nothing End Sub Sub Focus_Select(objName,value) '根据value,默认定位option dim objselect value=Trim(value) if value="" then Exit Sub value=Lcase(value) Set objselect=document.getElementbyID(objName) For Each it in objselect.Options if Lcase(Trim(it.value))=value then it.selected=True Exit Sub end if Next Set objselect=Nothing End Sub Sub Focus_ChekcBox(ByRef Obj,Value) dim tempStr,i tempStr=Lcase(Value) tempStr=Split(tempStr,",") if Value="" then Exit Sub For i=0 to Ubound(tempStr) if Lcase(Trim(Obj.value))=Trim(tempStr(i)) then Obj.checked=True end if Next End Sub Sub Focus_Radio(ObjName,value) dim objselect value=Trim(value) if value="" then Exit Sub value=Lcase(value) Set objselect=document.getElementsbyName(objName) For Each it in objselect if Lcase(Trim(it.value))=value then it.checked=True Exit Sub end if Next Set objselect=Nothing End Sub Function getCheckBoxValue(ObjName) dim tempValue for each it in document.getElementsByName(ObjName) if it.checked then if tempValue="" then tempValue=it.Value else tempValue=tempValue&","&it.Value end if end if next getCheckBoxValue=tempValue End Function Function getRadioValue(sObjName) dim returnValue For Each it in Document.getElementsByName(sObjName) if it.checked then returnValue=CStr(it.Value) Exit For end if Next getRadioValue=returnValue End Function Sub CheckBox_SelectAll(ObjName) for each it in document.getElementsByName(ObjName) it.checked=True next End Sub Sub CheckBox_CancelAll(ObjName) for each it in document.getElementsByName(ObjName) it.checked=false next End Sub REM =============表单处理================ Function CNLength(sStr) dim i,tempWord,LenCounter LenCounter=0 For i=1 to Len(sStr) tempWord=Mid(sStr,i,1) if Asc(tempWord)>0 then LenCounter=LenCounter+1 else LenCounter=LenCounter+2 end if Next CNLength=LenCounter/2 End Function