/**
* a simple Tip class
*
* @type : class
* @Parameter : a_sId: tip element id
* a_sStyle: the style of tip element
* a_sContent: the content of tip
* a_oObj: the parnet of tip element
* @author : cjj(http://www.imcjj.com)
* @version : v0.1.0 build 20080609
* @memo : none
* -----------------------------------
* Copyright (c) cjj
*/
function Tip(a_sId, a_sStyle, a_sContent, a_oObj, a_sElementName, a_sStruct) {
this._insertDirection=1;
this._struct="{#text#}";
this._elementName="SPAN";
if(a_sElementName) this._elementName=a_sElementName;
if(a_sStruct) this._struct=a_sStruct;// this._struct='
{#text#}
\n
\n';
this._tips={};
this.add=Tip_Add;
this.show=Tip_Show;
this.hide=Tip_Hide;
if(a_sId&&a_sStyle&&a_sContent) {
var oElement=document.getElementById(a_sId);
if(oElement) {
if(a_bStatus) oElement.style.display='';
else oElement.style.display='none';
if(oElement.getElementsByTagName("DIV").length<1) oElement.innerHTML=this._struct.replace(/\{#text#\}/g,a_sContent);
}
else {
oElement=document.createElement("span");// oElement=document.createElement("DIV");
oElement.className=a_sStyle;
oElement.id=a_sId;
oElement.innerHTML=this._struct.replace(/\{#text#\}/g,a_sContent);
if(typeof(a_oObj)!="object") document.body.appendChild(oElement);
else
switch(this._insertDirection){//节点插入的方向
case 0:a_oObj.insertBefore(oElement,a_oObj.firstChild);break;
default:a_oObj.appendChild(oElement);break;
}
}
this._tips[a_sId]=oElement;
}
}
//add new tip
function Tip_Add(a_sId, a_sStyle, a_sContent, a_oObj, a_sElementName, a_sStruct) {
if(!(a_sId&&a_sStyle&&a_sContent)) return;
var oElement=document.getElementById(a_sId);
var sStruct=this._struct;
if(a_sStruct) sStruct=a_sStruct;
if(oElement) {
// if(a_bStatus) oElement.style.display='';
// else oElement.style.display='none';
oElement.style.display='none';
if(oElement.getElementsByTagName("DIV").length<1) oElement.innerHTML=sStruct.replace(/\{#text#\}/g,a_sContent);
}
else {
if(a_sElementName) oElement=document.createElement(a_sElementName);
else oElement=document.createElement(this._elementName);
oElement.className=a_sStyle;
oElement.id=a_sId;
oElement.innerHTML=sStruct.replace(/\{#text#\}/g,a_sContent);
if(typeof(a_oObj)!="object") document.body.appendChild(oElement);
else
switch(this._insertDirection){
case 0:a_oObj.insertBefore(oElement,a_oObj.firstChild);break;
default:a_oObj.appendChild(oElement);break;
}
}
this._tips[a_sId]=oElement;
}
//show tip
function Tip_Show(a_sId, a_oObj) {
var oTip=this._tips[a_sId], oObj=null;
if(!oTip) return;
oTip.style.display='';
}
//hide tip
function Tip_Hide(a_sId) {
var oTip=this._tips[a_sId];
if(!oTip) return;
oTip.style.display = 'none';
}
/**
* FormValidate
*
* @type : class
* @parameter : a_sName, the form name attribute or id attribute
* @author : cjj(http://www.imcjj.com)
* @version : v1.0.0 build 20070613
* @memo :
* -----------------------------------
* Copyright (c) imcjj.com
*/
function FormValidate(a_sName) {
var oForm=document.getElementById(a_sName);
if(!oForm) oForm=document.forms[a_sName];
if(!oForm) return null;//未找到表单
this._form=oForm;
this._rules=[];//设置表单元素验证集合
this._classes={};//元素class
//默认支持的表单验证
this._validates={
"isNotNull":[/[\S]+/,"r",{"style":"tip_01","content":"不能为空"}],
"isNumber":[/^\d+$/,"r",{"style":"tip_01","content":"是否全部为数字"}],
"isLetter":[/^[A-Za-z0-9 ]+$/,"r",{"style":"tip_01","content":"是否全部为字母?"}],
"isChinese":[/^[\u0391-\uFFE5]+$/,"r",{"style":"tip_01","content":"是否全部为汉字?"}],
"isMail":[/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,"r",{"style":"tip_01","content":"邮件地址是否正确?"}],
"isMobile":[/^((\(\d{2,3}\))|(\d{3}\-))?13\d{9}$/,"r",{"style":"tip_01","content":"手机号码是否正确?"}],
"isTel":[/^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/,"r",{"style":"tip_01","content":"电话或传真号码是否正确?"}],
"isZip":[/^[1-9]\d{5}$/,"r",{"style":"tip_01","content":"邮政编码是否正确?"}],
"isURL":[/^http|ftp:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/,"r",{"style":"tip_01","content":"网页地址是否正确?"}],
"isExists":[_isExists,"f",{"style":"tip_01","content":"相关信息已经存在"}],
"isLen":[_isLen,"f",{"style":"tip_01","content":"输入的字符长度不符合要求"}],
"isSame":[_isSame,"f",{"style":"tip_01","content":"输入的内容不同"}]
};
this._validate=FormValidate_Validate;
this.addRule=FormValidate_addRule;//增加表单元素验证规则
this.addValidate=FormValidate_addValidate;//添加外部验证
this.listen=FormValidate_Listen;//表单验证监听
}
/**
* 表单验证外部函数
*
* @type:公共函数
* @Parameter:a_sName:验证名称
* a_oHandler:执行验证功能的对象
* a_sType: 验证功能对象的类型 r:正则, f:函数
* a_oTip: 验证失败时的提示信息对象
* @return:无
* @note:
*/
function FormValidate_addValidate(a_sName,a_oHandler,a_sType, a_oTip) {this._validates[a_sName]=[a_oHandler,a_sType, a_oTip];}
/**
* 监听表单,当表单提交时,进行验证
*
* @type:公共函数
* @Parameter:无
* @return:无
* @note:
*/
function FormValidate_Listen() {
var oForm=this._form;
if(!oForm) return;
var oObj=this;
oForm.submit=function() {if(oObj._validate("FormValidate_#@submit")) oForm.submit();};
oForm.onsubmit=function(){if(oObj._validate("FormValidate_#@submit")) return true;return false;};
}
function FormValidate_Validate(a_oObj) {//表单提交时进行验证
var oValidates=this._validates, oRules=this._rules, oClasses=this._classes;
var oObj, oMsg, oRule, oElement, oValidate;
var bReturn=true, bResult=true;
var aResults, aMsgs;
var sStr="", sTag="", sIndex="", sType="", siteid="";
var j=0;
if(typeof(a_oObj)=="string") {sType=a_oObj;}
if(sType=="") {
oRule = a_oObj;
oElement=oRule.Element;
sTag=oElement.tagName;
siteid="";
if(oElement.id) siteid=oElement.id;
bResult=false;
switch(sTag) {
case "SELECT":
sStr=oElement.options[oElement.selectedIndex].value;
break;
default:
sStr=oElement.value;
break;
}
if(!oRule.mustInput) {//选填
oValidate=oValidates["isNotNull"];//是否输入内容
if(!oValidate[0].test(sStr)) {oRule.Element.className=oClasses[siteid];oRule.tip.hide("tip_"+siteid);return;}
}
oValidate=oValidates[oRule.Name];
sStr=sStr.replace(/\'/g,"\\'");
switch(oValidate[1]) {
case "f"://函数
if(oRule.parameters.length>0) {bResult=eval("oValidate[0]("+ oRule.parameters.join(',') +",'"+sStr+"')");}
else {bResult=oValidate[0](sStr);}
break;
default://"r"
bResult=oValidate[0].test(sStr);
break;
}
oTip=oRule.tip;
oRule.validateResult=bResult;
oRules[i]=oRule;
if(!bResult) {//表单验证结果
bReturn=false;
oRule.Element.className="Msg_Background";
oTip.show("tip_"+siteid);
}
else {
oRule.Element.className=oClasses[siteid];
oTip.hide("tip_"+siteid);
}// end if
}
if(sType!=""){
for(var i=0;i0) {bResult=eval("oValidate[0]("+ oRule.parameters.join(',') +",'"+sStr+"')");}
else {bResult=oValidate[0](sStr);}
break;
default://"r"
switch(sTag) {
case "SELECT":
if(parseInt(sStr)>0) bResult=true;
break;
default:
bResult=oValidate[0].test(sStr);
break;
}
break;
}
oTip=oRule.tip;
oRule.validateResult=bResult;
oRules[i]=oRule;
if(!bResult) {//表单验证结果
bReturn=false;
oRule.Element.className="Msg_Background";
oTip.show("tip_"+siteid);
}
else {
oRule.Element.className=oClasses[siteid];
oTip.hide("tip_"+siteid);
}// end if
}//end for
}//end if a_sType
this._rules=oRules;
return bReturn;
}
/**
* 为表单增加验证规则
*
* @type:公共函数
* @Parameter:a_sType:验证类型
* a_sElement: 元素名称
* @return:无
* @note:
*/
function FormValidate_addRule(a_sRuleName, a_sElementName, a_aParameters, a_oTip) {
var oRules = this._rules;
var oObj=this;
var oForm=this._form;
var iLen = oRules.length, i;
var oElement, oRule, oClasses=this._classes, oValidates=this._validates, oElements=[], oElmIDs=a_sElementName.split(",");
var sIndex="", siteid;
for(i=0;i0) break;
}
if(sIndex=="") {return;}
if(sIndex.substring(0,1)=="*") bMustInput=true;
if(bMustInput) siteid=sIndex.substring(1,sIndex.length);
else siteid=sIndex;
oPNode=a_oElements[sIndex].parentNode;
oTip= new Tip("tip_"+siteid, a_oTip.style, a_oTip.content, oPNode);
oTip.hide("tip_"+siteid);
this.Elements=a_oElements;
this.Element=a_oElements[sIndex];
this.mustInput=false;//默认非必填
if(sIndex.substring(0,1)=="*") {this.mustInput=true;}//必填
this.Name=a_sType;
this.tip=oTip;
this.validateResult=null;
this.parameters=a_aParameters;
}
/**
* 验证内容是否已存在,ajax应用
*
* @type:函数
* @Parameter:a_sURL
* @return:无
* @note:
*/
function _isExists(a_sURL,a_sValue) {
var bResult=false;
var oXmlHttp = null;
if (window.XMLHttpRequest) {//non IE
oXmlHttp=new XMLHttpRequest();
if(oXmlHttp.overrideMimeType){oXmlHttp.overrideMimeType('text/plain');}
}
else if(window.ActiveXObject) { //IE
try { oXmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
catch (e) {
try { oXmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
catch (e) {}
}
}
oXmlHttp.open("post",a_sURL+"?"+a_sValue,false);
oXmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
oXmlHttp.setRequestHeader("Content-Length",a_sValue.length);
oXmlHttp.send(a_sValue);
if (oXmlHttp.readyState == 4) {
if (oXmlHttp.status == 200) {
if(oXmlHttp.responseText=="0"){bResult=true;}
}
else { alert("HTTP错误: "+oXmlHttp.status);}
}
if(oXmlHttp.responseText=="0"){bResult=true;}
return bResult;
}// end function _isExists
function _isLen(a_iMinLen,a_iMaxLen,a_sValue){//验证内容长度
var oReg=new RegExp("^\\S{"+a_iMinLen+","+a_iMaxLen+"}$","g");
return oReg.test(a_sValue);
}
function _isSame(a_sElementId, a_sValue) {
var bResult=false;
var oElement=document.getElementById(a_sElementId);
if(!oElement) return bResult;
if(oElement.value==a_sValue) bResult=true;
return bResult;
}