现在的位置: 首页 > 综合 > 正文

shawl.qiu javascript 表单验证类/Validator Class v1.2

2012年03月31日 ⁄ 综合 ⁄ 共 14400字 ⁄ 字号 评论关闭

 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. 调用演示

  1. shawl.qiu framework javascript client-side
  2. Class Validator
  3. ---/------------------------------------------------------------------------------
  4. Version: 1.2
  5. 目录:
  6. 1. 调用
  7. 1.1 调用 1
  8. 1.2 调用 2
  9. ---/---------------------------------------
  10. 内容:
  11. 1. 调用
  12. 1.1 调用 1
  13. <script type="text/javascript">
  14. //<![CDATA[
  15.  im.go('Form.Validator_v1-2');
  16.  //im.help('Form.TextFile.Validator', true);
  17. //]]>
  18. </script>
  19. <script type="text/javascript">
  20.  var valid=new Validator();
  21.   
  22.   valid.Form.Id = 'SubmiteFormId';
  23.   valid.Form.Action='';
  24.   
  25.   //valid.Form.Message.Onsubmit = '现在提交数据?';
  26.   //valid.Form.Message.Onreset = '现在重置数据?';
  27.   
  28.   valid.Form.Method.Unvalid = "get";
  29.   valid.Form.Method.Valid = "post";
  30.   
  31.   valid.Check.NoEmpty = 'mrpname##大名不能为空!,mrpsubject##主题不能为空!'+
  32.    ',mrptext##内容不能为空!,mrpemail##电子邮件不能为空!!';
  33.    
  34.   valid.Check.MinLength = 'mrppwd:6##密码不能小于六位!,checkeode:4##验证码不能小于4位!';
  35.   valid.Check.MaxLength = 'mrppwd:16##密码不能大于十六位!,checkeode:4##验证码不能大于4位';
  36.   
  37.   valid.Check.Email = "mrpemail##电子邮件格式不正确!,mrpmsn##MSN格式不正确!";
  38.   valid.Check.Url = "mrphomepage##请输入正确的URL地址!";
  39.   valid.Check.Number = "mrpqq##QQ号必须为数字!,checkeode##验证码必须为数字!";
  40.   valid.Check.Identical = "mrppwd:mrprepwd##密码与确认密码不同!";
  41.   
  42.   valid.go();
  43.   
  44.   valid=null;
  45. //]]>
  46. </script>
  47. 1.2 调用 2
  48. <script type="text/javascript">
  49.  var valid=new Validator();
  50.   
  51.   valid.Form.Id = 'SubmiteFormId';
  52.   valid.Form.Action='';
  53.   
  54.   //valid.Form.Message.Onsubmit = '现在提交数据?';
  55.   //valid.Form.Message.Onreset = '现在重置数据?';
  56.   
  57.   valid.Form.Method.Unvalid = "get";
  58.   valid.Form.Method.Valid = "post";
  59.   
  60.   valid.Check.NoEmpty = 'mrpname##大名不能为空!,mrpsubject##主题不能为空!'+
  61.    ',mrptext##内容不能为空!,mrpemail##电子邮件不能为空!!';
  62.    
  63.   valid.Check.MinLength = 'mrppwd:6##密码不能小于六位!,checkeode:4##验证码不能小于4位!';
  64.   valid.Check.MaxLength = 'mrppwd:16##密码不能大于十六位!,checkeode:4##验证码不能大于4位';
  65.   
  66.   valid.Check.Email = "mrpemail##电子邮件格式不正确!,mrpmsn##MSN格式不正确!";
  67.   valid.Check.Url = "mrphomepage##请输入正确的URL地址!";
  68.   valid.Check.Number = "mrpqq##QQ号必须为数字!,checkeode##验证码必须为数字!";
  69.   valid.Check.Identical = "mrppwd:mrprepwd##密码与确认密码不同!";
  70.   
  71.   valid.go();
  72.   
  73.   valid=null;
  74. //]]>
  75. </script>

2. 类 Validator 及使用演示 (x.htm)

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <!-- DW6 -->
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>shawl.qiu template</title>
  7. <style type="text/css">
  8. /* <![CDATA[ */
  9. /* ]]> */
  10. </style>
  11. <script type="text/javascript">
  12. //<![CDATA[
  13.     if (navigator.appName=="Microsoft Internet Explorer") {
  14.         //最大化窗口
  15.         self.moveTo(-5,-5)
  16.         self.resizeTo(screen.availWidth +8,screen.availHeight+8)
  17.         //这个脚本定义的宽度其实比原窗口还要大那么一点.
  18.     }
  19. //]]>
  20. </script>
  21. <script type="text/javascript">
  22. //<![CDATA[
  23.  onload = 
  24.   function()
  25.   {
  26.   
  27.   }
  28.         
  29. // shawl.qiu JavaScript Document
  30.  /*-----------------------------------------------------------------------------------*/
  31.   * shawl.qiu javascript 表单验证类 v1.2
  32.  /*-----------------------------------------------------------------------------------*/
  33.  //---------------------------------begin class Validator()-------------------------------//
  34.  function Validator()
  35.  { // shawl.qiu code
  36.   //------------------------------------begin public variable
  37.   //---------------begin about 
  38.   this.au = {}
  39.   this.au.Subject = "shawl.qiu javascript 表单验证类";
  40.   this.au.Version = "v1.2";
  41.   this.au.Name = "shawl.qiu";
  42.   this.au.Email = "shawl.qiu@gmail.com";
  43.   this.au.Blog = "http://blog.csdn.net/btbtd";
  44.   this.au.CreatedDate = "2006-12-31";
  45.   this.au.Update = {};
  46.   this.au.Update["1"] = "2007-6-2 v1.1";
  47.   this.au.Update["2"] = "2007-6-5 v1.2";
  48.   //---------------end about
  49.   this.Form =  {}
  50.   this.Form.Id = "";
  51.   this.Form.Action = "";
  52.   
  53.   this.Form.Message = {};
  54.   this.Form.Message.Onsubmit = "";
  55.   this.Form.Message.Onreset = "";
  56.   
  57.   this.Form.Method = {}
  58.   this.Form.Method.Unvalid = "";
  59.   this.Form.Method.Valid = "";
  60.     
  61.   this.Check = {}
  62.   
  63.   this.Check.NoEmpty = "";
  64.   
  65.   this.Check.MinLength = "";
  66.   this.Check.MaxLength = "";
  67.   
  68.   this.Check.Email = "";
  69.   this.Check.Url = "";
  70.   this.Check.Number = "";
  71.   this.Check.Username = "";
  72.   
  73.   this.Check.Identical = ""; // item1:item2##msg,item1:item2:item3:item4...##msg
  74.   
  75.   this.Check.Tag = {}
  76.   this.Check.Tag.Fields = "";
  77.   this.Check.Tag.Delimiter = ",";
  78.   this.Check.Tag.Trim = true;
  79.   
  80.   this.Func = {}
  81.   this.Func.ClearHtml = "";
  82.   this.Func.ClearRepeats = "";
  83.   this.Func.ClearTextBoxSubmit = true;
  84.   
  85.   this.Func.Encode = {}
  86.   this.Func.Encode.Component = "";
  87.   this.Delimiter = {}
  88.   this.Delimiter.Message = "##";
  89.   
  90.   //------------------------------------end public variable
  91.   this.go=
  92.    function()
  93.    {
  94.     if(Tl.Form.Id=='')alert('form id must be have a value!');
  95.     
  96.     var oForm=document.getElementById(Tl.Form.Id);
  97.  
  98.     if(oForm==null)
  99.     {
  100.      alert('could not get the form element!');
  101.      return false;
  102.     } // end for
  103.     
  104.     pForm = oForm;
  105.     
  106.     if(Tl.Form.Method.Unvalid!="") oForm.method = Tl.Form.Method.Unvalid;
  107.     
  108.     if(Tl.Func.ClearTextBoxSubmit)
  109.     {
  110.      fClearTextboxSubmite(oForm);
  111.     }
  112.      
  113.     var oTemp=oForm.onreset;
  114.     
  115.     oForm.onreset=
  116.      function()
  117.      {
  118.       var bReset = false;
  119.       try{bReset = oTemp();}catch(e){}
  120.       if(Tl.Form.Message.Onreset!='')
  121.       {
  122.        return confirm(Tl.Form.Message.Onreset);
  123.       }
  124.      } // end onreset
  125.      
  126.      var oTemp_=oForm.onsubmit;
  127.      oForm.onsubmit=
  128.       function()
  129.       {
  130.        if(Tl.Form.Message.Onsubmit!='')
  131.        {
  132.         var bReq=confirm(Tl.Form.Message.Onsubmit);
  133.         if(!bReq)return false;
  134.        }
  135.       
  136.       var tlForm=this;
  137.       var arTemp=[];
  138.       var bBln=true;
  139.       
  140.       for(var i=0, j=this.length; i<j; i++)
  141.       {
  142.        if(this[i].name=='')continue;
  143.        
  144.        this[i].value=fStrTrim(this[i].value);
  145.        if(this[i].name!='')
  146.        {
  147.         
  148.         var re=new RegExp('//b'+this[i].name+'//b', 'i'); 
  149.         
  150.         var ReGetMsg = 
  151.          new RegExp("^.*?(//b"+this[i].name+")//b"+Tl.Delimiter.Message+"(.*?)(/,|$).*", "i");
  152.         
  153.         //  valid.Check.MaxLength='checkcode:4##验证码不能大于四个字符!'
  154.         var ReGetMsg1 = 
  155.          new RegExp("^.*?(//b"+this[i].name+")//b/://d+"+Tl.Delimiter.Message+"(.*?)(/,|$).*", "i");
  156.         
  157.         if(re.test(Tl.Check.NoEmpty))
  158.         {
  159.          if(this[i].value=='')
  160.          {
  161.           bBln=false;
  162.           if(ReGetMsg.test(Tl.Check.NoEmpty))
  163.           {
  164.            var Msg = Tl.Check.NoEmpty.replace(ReGetMsg, "$2");
  165.            arTemp[arTemp.length] = Msg.fontcolor("red");
  166.           }
  167.           else
  168.           {
  169.            arTemp[arTemp.length]=this[i].name.bold().fontcolor('red')+': could not be empty!'
  170.           }
  171.          } // end if 2 1
  172.         } // end if 2
  173.          
  174.         if(re.test(Tl.Check.MinLength)&&this[i].value!="")
  175.         {
  176.          var re_=new RegExp('//b'+this[i].name+'/:(//d+)','i');
  177.         
  178.          Tl.Check.MinLength.replace
  179.          (
  180.           re_,
  181.           function($0,$1)
  182.           {
  183.            $1-=0;
  184.            if(tlForm[i].value.length<$1)
  185.            {
  186.             bBln=false;
  187.             if(ReGetMsg1.test(Tl.Check.MinLength))
  188.             {
  189.              var Msg = Tl.Check.MinLength.replace(ReGetMsg1, "$2");
  190.              arTemp[arTemp.length] = Msg.fontcolor("red");
  191.             }
  192.             else
  193.             {
  194.             arTemp[arTemp.length]=tlForm[i].name.bold().fontcolor('red')+
  195.              ': length must be >= '+$1+'.'
  196.             }
  197.            } // end if 2 1
  198.           }
  199.          );
  200.         } // end if 2
  201.         
  202.         if(re.test(Tl.Check.MaxLength)&&this[i].value!="")
  203.         {
  204.          var re_=new RegExp('//b'+this[i].name+'/:(//d+)','i');
  205.          Tl.Check.MaxLength.replace
  206.          (
  207.           re_,
  208.           function($0,$1)
  209.           {
  210.            $1-=0;
  211.            if(tlForm[i].value.length>$1)
  212.            {
  213.             bBln=false;
  214.             if(ReGetMsg1.test(Tl.Check.MaxLength))
  215.             {
  216.              var Msg = Tl.Check.MaxLength.replace(ReGetMsg1, "$2");
  217.              arTemp[arTemp.length] = Msg.fontcolor("red");
  218.             }
  219.             else
  220.             {
  221.             arTemp[arTemp.length]=tlForm[i].name.bold().fontcolor('red')+
  222.              ': length must be >= '+$1+'.'
  223.             }
  224.            } // end if 2 1
  225.           }
  226.          );
  227.         } // end if 2
  228.         
  229.         if(re.test(Tl.Check.Email)&&this[i].value!="")
  230.         {
  231.          var re_=/^/w+[.+/-]*?[/w]*?/@[/w]+([-.]/w+)*/./w+([-.]/w+)*$/i;
  232.          if(!re_.test(this[i].value))
  233.          {
  234.           bBln=false
  235.           if(ReGetMsg.test(Tl.Check.Email))
  236.           {
  237.            var Msg = Tl.Check.Email.replace(ReGetMsg, "$2");
  238.            arTemp[arTemp.length] = Msg.fontcolor("red");
  239.           }
  240.           else
  241.           {
  242.            arTemp[arTemp.length]=tlForm[i].name.bold().fontcolor('red')
  243.             +': invalid e-mail address ('+tlForm[i].value.fontcolor('red')+'). '+
  244.             '<br/>correct e-mail sample: shawl.qiu@gmail.com';
  245.           }
  246.          }
  247.         } // end if 2
  248.         
  249.         if(re.test(Tl.Func.Encode.Component)&&Tl.Func.Encode.Component!="")
  250.         {
  251.          if(this[i].value!="")
  252.          {
  253.           this[i].value = encodeURIComponent(this[i].value);
  254.          }
  255.         } // end if 2
  256.         
  257.         if(re.test(Tl.Check.Url)&&this[i].value!="")
  258.         {
  259.          var re_=/^(f|ht)t(p|ps)/:////[/w/-]+?/.[/S]+$/i;
  260.          if(!re_.test(this[i].value))
  261.          {
  262.           bBln=false;
  263.           
  264.           if(ReGetMsg.test(Tl.Check.Url))
  265.           {
  266.            var Msg = Tl.Check.Url.replace(ReGetMsg, "$2");
  267.            arTemp[arTemp.length] = Msg.fontcolor("red");
  268.           }
  269.           else
  270.           {
  271.           arTemp[arTemp.length]=tlForm[i].name.bold().fontcolor('red')
  272.            +': invalid url address ('+tlForm[i].value.fontcolor('red')+'). '+
  273.            '<br/>correct url sample: http://www.google.com/';
  274.           }
  275.          }
  276.         } // end if 2
  277.         
  278.         if(re.test(Tl.Check.Username))
  279.         {
  280.          if(/[^a-z0-9/_/-/.]/i.test(this[i].value))
  281.          {
  282.           bBln=false;
  283.           var ReGetMsg = 
  284.            new RegExp("^.*?(//b"+this[i].name+")//b"+Tl.Delimiter.Message+"(.*?)(/,|$).*", "i");
  285.           
  286.           if(ReGetMsg.test(Tl.Check.Username))
  287.           {
  288.            var Msg = Tl.Check.Username.replace(ReGetMsg, "$2");
  289.            arTemp[arTemp.length] = Msg.fontcolor("red");
  290.           }
  291.           else
  292.           {
  293.            arTemp[arTemp.length]=this[i].name.bold().fontcolor('red')+
  294.             ': character must be in '+('[a-z A-Z 0-9 - _ .]').fontcolor('blue');
  295.           }
  296.          } // end if 2 1
  297.         } // end if 2
  298.         
  299.         if(re.test(Tl.Check.Number))
  300.         {
  301.          if(/[^0-9]/i.test(this[i].value))
  302.          {
  303.           bBln=false;
  304.           if(ReGetMsg.test(Tl.Check.Number))
  305.           {
  306.            var Msg = Tl.Check.Number.replace(ReGetMsg, "$2");
  307.            arTemp[arTemp.length] = Msg.fontcolor("red");
  308.           }
  309.           else
  310.           {
  311.            arTemp[arTemp.length]=this[i].name.bold().fontcolor('red')+
  312.             ': character must be digital '+('[0123456789]').fontcolor('blue');
  313.           }
  314.          } // end if 2 1
  315.         } // end if 2
  316.         
  317.         if(re.test(Tl.Check.Tag.Fields)&&Tl.Check.Tag.Fields!="")
  318.         {
  319.          var arTag = this[i].value.split(Tl.Check.Tag.Delimiter);
  320.          var iLen = arTag.length;
  321.          var iCount = 0;
  322.          while(iCount<iLen)
  323.          {
  324.           if(Tl.Check.Tag.Trim)
  325.           {
  326.            arTag[iCount] = fStrTrim(arTag[iCount]);
  327.           }
  328.           iCount++;
  329.          } // end while
  330.          
  331.          arTag = fClearArrayRepeats(arTag);
  332.          
  333.          this[i].value = arTag.join(Tl.Check.Tag.Delimiter);
  334.         } // end if 2
  335.         
  336.         if(re.test(Tl.Func.ClearHtml))
  337.         {
  338.          this[i].value=fClearHtml(this[i].value);
  339.         } // end if 2
  340.         
  341.         if(re.test(Tl.Func.ClearRepeats))
  342.         {
  343.          this[i].value=fClearArrayRepeats(this[i].value.split(','));
  344.         } // end if 2
  345.  
  346.        } // end if 1
  347.       } // end for 1
  348.       
  349.       if(Tl.Check.Identical!='')
  350.       {
  351.        var IdcSet = Tl.Check.Identical.split("'");
  352.        var IdcSetCount = 0;
  353.        var IdcSetLen = IdcSet.length;
  354.        
  355.        OutIdc:
  356.        while(IdcSetCount<IdcSetLen)
  357.        {
  358.         var MsgSet = IdcSet[IdcSetCount].split(Tl.Delimiter.Message);
  359.         if(MsgSet.length<1||MsgSet.length>2)
  360.         {
  361.          IdcSetCount++;
  362.          continue;
  363.         }
  364.         if(MsgSet.length==1) MsgSet[1] = "密码和输入密码不同!";
  365.         
  366.         var ItemSet = MsgSet[0].split(":");
  367.         
  368.         if(ItemSet.length<2)
  369.         {
  370.          IdcSetCount++;
  371.          continue;
  372.         }
  373.         
  374.         var ValueSet = [];
  375.         
  376.         var iLen = ItemSet.length;
  377.         var iCount = 0;
  378.         var iItemCount = 0;
  379.         while(iCount<iLen)
  380.         {
  381.          var TempObj = pForm[ItemSet[iCount]];
  382.          if(TempObj==null)
  383.          {
  384.           var TempMsg = "无法获得名为 /""+(ItemSet.join()).bold()+"/" 的一个或多个表单元素!";
  385.           bBln = false;
  386.           arTemp[arTemp.length] = TempMsg.fontcolor("red");
  387.           break OutIdc;
  388.          }
  389.          ValueSet[iItemCount++] = TempObj.value;
  390.          iCount++;
  391.         }
  392.         
  393.         var bIdentical = fComparisonArrayItem(ValueSet);
  394.         
  395.         if(!bIdentical)
  396.         {
  397.          bBln = false;
  398.          arTemp[arTemp.length] = MsgSet[1].fontcolor("red");
  399.         }
  400.         
  401.         IdcSetCount++;
  402.        }
  403.       } // end if
  404.       
  405.       if(bBln)
  406.       {
  407.        fRemoveSelf(pReportId);
  408.        
  409.        if(this.action=='')this.action=Tl.Form.Action;
  410.        if(Tl.Form.Method.Valid!='')
  411.        {
  412.         this.method=Tl.Form.Method.Valid;
  413.        }
  414.        
  415.        var bSubmite = true;
  416.        
  417.        try{bSubmite = oTemp_();}catch(e){}
  418.        
  419.        return bSubmite;
  420.       } 
  421.       else
  422.       {
  423.        fRemoveSelf(pReportId);
  424.        
  425.        var sResult = "<ol style='padding:0px 0px 0px 40px; text-align:left;'>";
  426.        sResult += "<label style='font-weight:bold;padding:5px 0px; display:block;'>"
  427.         +"提交数据时出错, 出错原因!</label>";
  428.        
  429.        var iarTemplen = arTemp.length;
  430.        var iarTempCount = 0;
  431.        
  432.        while(iarTempCount<iarTemplen)
  433.        {
  434.         sResult += "<li>"+arTemp[iarTempCount]+"</li>";
  435.         iarTempCount++
  436.        }
  437.        sResult+="</ol>";
  438.        
  439.        var oPrnt=this.parentNode;
  440.        var oTemp=document.createElement('div');
  441.         oTemp.style.width='99%';
  442.         oTemp.style.margin='10px 2px';
  443.         oTemp.id=pReportId;
  444.         oTemp.innerHTML=sResult;

抱歉!评论已关闭.