shawl.qiu javascript 表单验证类/Validator Class v1.2
说明:
本类作用是验证表单输入数据是否符合要求.
特点:
从 form method 设置, 比如, 你要使用POST提交, 那就在FORM 设置成 GET,
调用时设置为:
valid.Form.Method.Unvalid = "get";
valid.Form.Method.Valid = "post";
未验证时METHOD 是 GET, 验证后, 如果合法, 则是POST.
还有很多调用功能, 不一一介绍, 看调用演示.
PS: 最近阴雨天气整得鄙人精神全无....应该会较少写博.
不过最近倒是升级以前的N多旧版本至2.0, 至于发布不发布, 尚是未知数.
因那些东西都比较成熟了, 需要考虑到利益问题.
目录:
1. 调用演示
2. 类 Validator 及使用演示 (x.htm)
shawl.qiu
2007-06-12
http://blog.csdn.net/btbtd
内容:
1. 调用演示
- shawl.qiu framework javascript client-side
- Class Validator
- ---/------------------------------------------------------------------------------
- Version: 1.2
- 目录:
- 1. 调用
- 1.1 调用 1
- 1.2 调用 2
- ---/---------------------------------------
- 内容:
- 1. 调用
- 1.1 调用 1
- <script type="text/javascript">
- //<![CDATA[
- im.go('Form.Validator_v1-2');
- //im.help('Form.TextFile.Validator', true);
- //]]>
- </script>
- <script type="text/javascript">
- var valid=new Validator();
- valid.Form.Id = 'SubmiteFormId';
- valid.Form.Action='';
- //valid.Form.Message.Onsubmit = '现在提交数据?';
- //valid.Form.Message.Onreset = '现在重置数据?';
- valid.Form.Method.Unvalid = "get";
- valid.Form.Method.Valid = "post";
- valid.Check.NoEmpty = 'mrpname##大名不能为空!,mrpsubject##主题不能为空!'+
- ',mrptext##内容不能为空!,mrpemail##电子邮件不能为空!!';
- valid.Check.MinLength = 'mrppwd:6##密码不能小于六位!,checkeode:4##验证码不能小于4位!';
- valid.Check.MaxLength = 'mrppwd:16##密码不能大于十六位!,checkeode:4##验证码不能大于4位';
- valid.Check.Email = "mrpemail##电子邮件格式不正确!,mrpmsn##MSN格式不正确!";
- valid.Check.Url = "mrphomepage##请输入正确的URL地址!";
- valid.Check.Number = "mrpqq##QQ号必须为数字!,checkeode##验证码必须为数字!";
- valid.Check.Identical = "mrppwd:mrprepwd##密码与确认密码不同!";
- valid.go();
- valid=null;
- //]]>
- </script>
- 1.2 调用 2
- <script type="text/javascript">
- var valid=new Validator();
- valid.Form.Id = 'SubmiteFormId';
- valid.Form.Action='';
- //valid.Form.Message.Onsubmit = '现在提交数据?';
- //valid.Form.Message.Onreset = '现在重置数据?';
- valid.Form.Method.Unvalid = "get";
- valid.Form.Method.Valid = "post";
- valid.Check.NoEmpty = 'mrpname##大名不能为空!,mrpsubject##主题不能为空!'+
- ',mrptext##内容不能为空!,mrpemail##电子邮件不能为空!!';
- valid.Check.MinLength = 'mrppwd:6##密码不能小于六位!,checkeode:4##验证码不能小于4位!';
- valid.Check.MaxLength = 'mrppwd:16##密码不能大于十六位!,checkeode:4##验证码不能大于4位';
- valid.Check.Email = "mrpemail##电子邮件格式不正确!,mrpmsn##MSN格式不正确!";
- valid.Check.Url = "mrphomepage##请输入正确的URL地址!";
- valid.Check.Number = "mrpqq##QQ号必须为数字!,checkeode##验证码必须为数字!";
- valid.Check.Identical = "mrppwd:mrprepwd##密码与确认密码不同!";
- valid.go();
- valid=null;
- //]]>
- </script>
2. 类 Validator 及使用演示 (x.htm)
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <!-- DW6 -->
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>shawl.qiu template</title>
- <style type="text/css">
- /* <![CDATA[ */
- /* ]]> */
- </style>
- <script type="text/javascript">
- //<![CDATA[
- if (navigator.appName=="Microsoft Internet Explorer") {
- //最大化窗口
- self.moveTo(-5,-5)
- self.resizeTo(screen.availWidth +8,screen.availHeight+8)
- //这个脚本定义的宽度其实比原窗口还要大那么一点.
- }
- //]]>
- </script>
- <script type="text/javascript">
- //<![CDATA[
- onload =
- function()
- {
- }
- // shawl.qiu JavaScript Document
- /*-----------------------------------------------------------------------------------*/
- * shawl.qiu javascript 表单验证类 v1.2
- /*-----------------------------------------------------------------------------------*/
- //---------------------------------begin class Validator()-------------------------------//
- function Validator()
- { // shawl.qiu code
- //------------------------------------begin public variable
- //---------------begin about
- this.au = {}
- this.au.Subject = "shawl.qiu javascript 表单验证类";
- this.au.Version = "v1.2";
- this.au.Name = "shawl.qiu";
- this.au.Email = "shawl.qiu@gmail.com";
- this.au.Blog = "http://blog.csdn.net/btbtd";
- this.au.CreatedDate = "2006-12-31";
- this.au.Update = {};
- this.au.Update["1"] = "2007-6-2 v1.1";
- this.au.Update["2"] = "2007-6-5 v1.2";
- //---------------end about
- this.Form = {}
- this.Form.Id = "";
- this.Form.Action = "";
- this.Form.Message = {};
- this.Form.Message.Onsubmit = "";
- this.Form.Message.Onreset = "";
- this.Form.Method = {}
- this.Form.Method.Unvalid = "";
- this.Form.Method.Valid = "";
- this.Check = {}
- this.Check.NoEmpty = "";
- this.Check.MinLength = "";
- this.Check.MaxLength = "";
- this.Check.Email = "";
- this.Check.Url = "";
- this.Check.Number = "";
- this.Check.Username = "";
- this.Check.Identical = ""; // item1:item2##msg,item1:item2:item3:item4...##msg
- this.Check.Tag = {}
- this.Check.Tag.Fields = "";
- this.Check.Tag.Delimiter = ",";
- this.Check.Tag.Trim = true;
- this.Func = {}
- this.Func.ClearHtml = "";
- this.Func.ClearRepeats = "";
- this.Func.ClearTextBoxSubmit = true;
- this.Func.Encode = {}
- this.Func.Encode.Component = "";
- this.Delimiter = {}
- this.Delimiter.Message = "##";
- //------------------------------------end public variable
- this.go=
- function()
- {
- if(Tl.Form.Id=='')alert('form id must be have a value!');
- var oForm=document.getElementById(Tl.Form.Id);
- if(oForm==null)
- {
- alert('could not get the form element!');
- return false;
- } // end for
- pForm = oForm;
- if(Tl.Form.Method.Unvalid!="") oForm.method = Tl.Form.Method.Unvalid;
- if(Tl.Func.ClearTextBoxSubmit)
- {
- fClearTextboxSubmite(oForm);
- }
- var oTemp=oForm.onreset;
- oForm.onreset=
- function()
- {
- var bReset = false;
- try{bReset = oTemp();}catch(e){}
- if(Tl.Form.Message.Onreset!='')
- {
- return confirm(Tl.Form.Message.Onreset);
- }
- } // end onreset
- var oTemp_=oForm.onsubmit;
- oForm.onsubmit=
- function()
- {
- if(Tl.Form.Message.Onsubmit!='')
- {
- var bReq=confirm(Tl.Form.Message.Onsubmit);
- if(!bReq)return false;
- }
- var tlForm=this;
- var arTemp=[];
- var bBln=true;
- for(var i=0, j=this.length; i<j; i++)
- {
- if(this[i].name=='')continue;
- this[i].value=fStrTrim(this[i].value);
- if(this[i].name!='')
- {
- var re=new RegExp('//b'+this[i].name+'//b', 'i');
- var ReGetMsg =
- new RegExp("^.*?(//b"+this[i].name+")//b"+Tl.Delimiter.Message+"(.*?)(/,|$).*", "i");
- // valid.Check.MaxLength='checkcode:4##验证码不能大于四个字符!'
- var ReGetMsg1 =
- new RegExp("^.*?(//b"+this[i].name+")//b/://d+"+Tl.Delimiter.Message+"(.*?)(/,|$).*", "i");
- if(re.test(Tl.Check.NoEmpty))
- {
- if(this[i].value=='')
- {
- bBln=false;
- if(ReGetMsg.test(Tl.Check.NoEmpty))
- {
- var Msg = Tl.Check.NoEmpty.replace(ReGetMsg, "$2");
- arTemp[arTemp.length] = Msg.fontcolor("red");
- }
- else
- {
- arTemp[arTemp.length]=this[i].name.bold().fontcolor('red')+': could not be empty!'
- }
- } // end if 2 1
- } // end if 2
- if(re.test(Tl.Check.MinLength)&&this[i].value!="")
- {
- var re_=new RegExp('//b'+this[i].name+'/:(//d+)','i');
- Tl.Check.MinLength.replace
- (
- re_,
- function($0,$1)
- {
- $1-=0;
- if(tlForm[i].value.length<$1)
- {
- bBln=false;
- if(ReGetMsg1.test(Tl.Check.MinLength))
- {
- var Msg = Tl.Check.MinLength.replace(ReGetMsg1, "$2");
- arTemp[arTemp.length] = Msg.fontcolor("red");
- }
- else
- {
- arTemp[arTemp.length]=tlForm[i].name.bold().fontcolor('red')+
- ': length must be >= '+$1+'.'
- }
- } // end if 2 1
- }
- );
- } // end if 2
- if(re.test(Tl.Check.MaxLength)&&this[i].value!="")
- {
- var re_=new RegExp('//b'+this[i].name+'/:(//d+)','i');
- Tl.Check.MaxLength.replace
- (
- re_,
- function($0,$1)
- {
- $1-=0;
- if(tlForm[i].value.length>$1)
- {
- bBln=false;
- if(ReGetMsg1.test(Tl.Check.MaxLength))
- {
- var Msg = Tl.Check.MaxLength.replace(ReGetMsg1, "$2");
- arTemp[arTemp.length] = Msg.fontcolor("red");
- }
- else
- {
- arTemp[arTemp.length]=tlForm[i].name.bold().fontcolor('red')+
- ': length must be >= '+$1+'.'
- }
- } // end if 2 1
- }
- );
- } // end if 2
- if(re.test(Tl.Check.Email)&&this[i].value!="")
- {
- var re_=/^/w+[.+/-]*?[/w]*?/@[/w]+([-.]/w+)*/./w+([-.]/w+)*$/i;
- if(!re_.test(this[i].value))
- {
- bBln=false;
- if(ReGetMsg.test(Tl.Check.Email))
- {
- var Msg = Tl.Check.Email.replace(ReGetMsg, "$2");
- arTemp[arTemp.length] = Msg.fontcolor("red");
- }
- else
- {
- arTemp[arTemp.length]=tlForm[i].name.bold().fontcolor('red')
- +': invalid e-mail address ('+tlForm[i].value.fontcolor('red')+'). '+
- '<br/>correct e-mail sample: shawl.qiu@gmail.com';
- }
- }
- } // end if 2
- if(re.test(Tl.Func.Encode.Component)&&Tl.Func.Encode.Component!="")
- {
- if(this[i].value!="")
- {
- this[i].value = encodeURIComponent(this[i].value);
- }
- } // end if 2
- if(re.test(Tl.Check.Url)&&this[i].value!="")
- {
- var re_=/^(f|ht)t(p|ps)/:////[/w/-]+?/.[/S]+$/i;
- if(!re_.test(this[i].value))
- {
- bBln=false;
- if(ReGetMsg.test(Tl.Check.Url))
- {
- var Msg = Tl.Check.Url.replace(ReGetMsg, "$2");
- arTemp[arTemp.length] = Msg.fontcolor("red");
- }
- else
- {
- arTemp[arTemp.length]=tlForm[i].name.bold().fontcolor('red')
- +': invalid url address ('+tlForm[i].value.fontcolor('red')+'). '+
- '<br/>correct url sample: http://www.google.com/';
- }
- }
- } // end if 2
- if(re.test(Tl.Check.Username))
- {
- if(/[^a-z0-9/_/-/.]/i.test(this[i].value))
- {
- bBln=false;
- var ReGetMsg =
- new RegExp("^.*?(//b"+this[i].name+")//b"+Tl.Delimiter.Message+"(.*?)(/,|$).*", "i");
- if(ReGetMsg.test(Tl.Check.Username))
- {
- var Msg = Tl.Check.Username.replace(ReGetMsg, "$2");
- arTemp[arTemp.length] = Msg.fontcolor("red");
- }
- else
- {
- arTemp[arTemp.length]=this[i].name.bold().fontcolor('red')+
- ': character must be in '+('[a-z A-Z 0-9 - _ .]').fontcolor('blue');
- }
- } // end if 2 1
- } // end if 2
- if(re.test(Tl.Check.Number))
- {
- if(/[^0-9]/i.test(this[i].value))
- {
- bBln=false;
- if(ReGetMsg.test(Tl.Check.Number))
- {
- var Msg = Tl.Check.Number.replace(ReGetMsg, "$2");
- arTemp[arTemp.length] = Msg.fontcolor("red");
- }
- else
- {
- arTemp[arTemp.length]=this[i].name.bold().fontcolor('red')+
- ': character must be digital '+('[0123456789]').fontcolor('blue');
- }
- } // end if 2 1
- } // end if 2
- if(re.test(Tl.Check.Tag.Fields)&&Tl.Check.Tag.Fields!="")
- {
- var arTag = this[i].value.split(Tl.Check.Tag.Delimiter);
- var iLen = arTag.length;
- var iCount = 0;
- while(iCount<iLen)
- {
- if(Tl.Check.Tag.Trim)
- {
- arTag[iCount] = fStrTrim(arTag[iCount]);
- }
- iCount++;
- } // end while
- arTag = fClearArrayRepeats(arTag);
- this[i].value = arTag.join(Tl.Check.Tag.Delimiter);
- } // end if 2
- if(re.test(Tl.Func.ClearHtml))
- {
- this[i].value=fClearHtml(this[i].value);
- } // end if 2
- if(re.test(Tl.Func.ClearRepeats))
- {
- this[i].value=fClearArrayRepeats(this[i].value.split(','));
- } // end if 2
- } // end if 1
- } // end for 1
- if(Tl.Check.Identical!='')
- {
- var IdcSet = Tl.Check.Identical.split("'");
- var IdcSetCount = 0;
- var IdcSetLen = IdcSet.length;
- OutIdc:
- while(IdcSetCount<IdcSetLen)
- {
- var MsgSet = IdcSet[IdcSetCount].split(Tl.Delimiter.Message);
- if(MsgSet.length<1||MsgSet.length>2)
- {
- IdcSetCount++;
- continue;
- }
- if(MsgSet.length==1) MsgSet[1] = "密码和输入密码不同!";
- var ItemSet = MsgSet[0].split(":");
- if(ItemSet.length<2)
- {
- IdcSetCount++;
- continue;
- }
- var ValueSet = [];
- var iLen = ItemSet.length;
- var iCount = 0;
- var iItemCount = 0;
- while(iCount<iLen)
- {
- var TempObj = pForm[ItemSet[iCount]];
- if(TempObj==null)
- {
- var TempMsg = "无法获得名为 /""+(ItemSet.join()).bold()+"/" 的一个或多个表单元素!";
- bBln = false;
- arTemp[arTemp.length] = TempMsg.fontcolor("red");
- break OutIdc;
- }
- ValueSet[iItemCount++] = TempObj.value;
- iCount++;
- }
- var bIdentical = fComparisonArrayItem(ValueSet);
- if(!bIdentical)
- {
- bBln = false;
- arTemp[arTemp.length] = MsgSet[1].fontcolor("red");
- }
- IdcSetCount++;
- }
- } // end if
- if(bBln)
- {
- fRemoveSelf(pReportId);
- if(this.action=='')this.action=Tl.Form.Action;
- if(Tl.Form.Method.Valid!='')
- {
- this.method=Tl.Form.Method.Valid;
- }
- var bSubmite = true;
- try{bSubmite = oTemp_();}catch(e){}
- return bSubmite;
- }
- else
- {
- fRemoveSelf(pReportId);
- var sResult = "<ol style='padding:0px 0px 0px 40px; text-align:left;'>";
- sResult += "<label style='font-weight:bold;padding:5px 0px; display:block;'>"
- +"提交数据时出错, 出错原因!</label>";
- var iarTemplen = arTemp.length;
- var iarTempCount = 0;
- while(iarTempCount<iarTemplen)
- {
- sResult += "<li>"+arTemp[iarTempCount]+"</li>";
- iarTempCount++
- }
- sResult+="</ol>";
- var oPrnt=this.parentNode;
- var oTemp=document.createElement('div');
- oTemp.style.width='99%';
- oTemp.style.margin='10px 2px';
- oTemp.id=pReportId;
- oTemp.innerHTML=sResult;