Advertisement
jeges

formChecker obect class

Jul 30th, 2011
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // for getElementById
  2. function $lm(obj){
  3.   if ( document.getElementById(obj) ) return document.getElementById(obj);
  4.   else return false;
  5. }
  6.  
  7. // first child of the same class
  8. function firstChildofClass(p,cl) {
  9.   if (!p || !p.childNodes.length)return;
  10.   var ch = p.childNodes;
  11.   for ( var i = 0; i < ch.length; i++ ){
  12.     if (gotClass(ch[i],cl))return ch[i];
  13.   }
  14.   return;
  15. }
  16.  
  17. // for firstChildOfClass
  18. function $fc(p, cl){
  19.   return firstChildofClass(p,cl);
  20. }
  21.  
  22. // checks if object has the defined class
  23. function gotClass(obj,cl) {
  24.     var r = new RegExp("\\b" + cl + "\\b", "i");
  25.     var ob = ( typeof(obj) == "object" ) ? obj : $lm(obj);
  26.     if ( ob && ob.className && ob.className.match(r) !== null ) return true;
  27.     return false;
  28. }
  29.  
  30. function getChildElementsByClassName(obj,cl){
  31.   var oc = ( obj != undefined ) ? obj.childNodes : document.body.childNodes;
  32.   var ch = [];
  33.   for ( var i = 0; i < oc.length; i++ ){
  34.     if (gotClass(oc[i],cl)) ch.push(oc[i]);
  35.   }
  36.   return ch;
  37. }
  38.  
  39. // for getChildElementsByClassName
  40. function $cc(obj,cl){
  41.   return getChildElementsByClassName(obj,cl);
  42. }
  43.  
  44. function pf(n){return parseFloat(n);}
  45.  
  46. /* formChecker class definition
  47.  * parameters:
  48.  * fm - the form to be verified
  49.  * rule - form rules in JSON format
  50.  * rule record (object) properties:
  51.  * sbj - input class
  52.  *      if the form contains more than one input of the same class, each one will be verified
  53.  * fnc - verifier function (these are methods of the chk() object class)
  54.  *      see functions for more details
  55.  * prm - value to verify against
  56.  *      can be a fixed value or a form elem value defined by class or id (css-like definitions -> ".myclass" or "#myid")
  57.  *
  58.  * usage:
  59.  *      1) link this script in your html head under some name (e.g. rule.js)
  60.  *      2) insert the following script type="text/javascript" tag into your html head
  61.  *      3) design your own rules
  62.  *      4) design your own checker functions into the chk() class definition if necessary
  63.  *
  64.  * code into your html head:
  65.  *
  66.   window.onload = function(){
  67.  
  68.     // the form's send button input
  69.     var sn = $lm("send");
  70.     // the form dom object to be verified
  71.     var fm = $lm("fm");
  72.     // log div object to hold the verification result
  73.     var lg = $lm("log");
  74.  
  75.     // send button event handler
  76.     sn.onclick = function(){
  77.         lg.innerHTML = "";
  78.         var fc = new formChecker(fm,myRule);
  79.         lg.innerHTML += fc.out();
  80.     }
  81. }
  82.    
  83. some rule examples:
  84.  
  85. myRule = {
  86.  
  87.     first: {
  88.       sbj: "nm",
  89.       fnc: "nn",
  90.       prm: "",
  91.       msg: "Name fields can not be empty."
  92.     },
  93.  
  94.     second: {
  95.       sbj: "yr",
  96.       fnc: "gt",
  97.       prm: ".minyr",
  98.       msg: "Must be over 18."
  99.     },
  100.  
  101.     third: {
  102.       sbj: "yr",
  103.       fnc: "lt",
  104.       prm: "#maxyr",
  105.       msg: "Must be under 99."
  106.     },
  107.  
  108.     fourth: {
  109.       sbj: "cn",
  110.       fnc: "nn",
  111.       prm: "",
  112.       msg: "Can not be empty."
  113.     },
  114.  
  115.     fifth: {
  116.       sbj: "cn",
  117.       fnc: "rg",
  118.       prm: ["^hu$","i"],
  119.       msg: "Must be hungarian."
  120.     },
  121.  
  122.     sixth: {
  123.       sbj: "cn",
  124.       fnc: "ex",
  125.       prm: "hu",
  126.       msg: "Must be hungarian."
  127.     },
  128.  
  129.     seventh: {
  130.       sbj: "cn",
  131.       fnc: "dd",
  132.       prm: "hu",
  133.       msg: "Errorous rule (no chk object method "dd")."
  134.     }
  135.  
  136.   }
  137.  
  138.  
  139.  *
  140. **/
  141.  
  142. function formChecker(fm,rule){
  143.  
  144.     if (!fm) return;
  145.  
  146.     this.fm = fm;       // form DOM object
  147.     this.rule = rule;   // rule JSON object-list
  148.     this.res = [];      // result array
  149.  
  150.     this.chk = function(){
  151.  
  152.         var r,val,lm,c,prm;
  153.  
  154.         // rules
  155.         for (var i in this.rule){
  156.  
  157.             r = this.rule[i];
  158.  
  159.             // check only if object exists
  160.             if ( $fc(this.fm,r.sbj) ){
  161.  
  162.                 // handle parameters
  163.                 // only try to find css-like object reference, if not number and not regexp
  164.                 // css-style DOM reference lookup with ".myclass" and "#myid"
  165.  
  166.                 prm = r.prm;
  167.                
  168.                 if ( isNaN(prm) && r.fnc != "rg" ){
  169.  
  170.                     // ".myclass"
  171.                     if ( prm.match(/^\.\w+$/i) != null ){
  172.  
  173.                         var tst = true;
  174.                         // only first object of the same class
  175.                         if ( $fc( this.fm , prm.substr(1) ) )prm = $fc( this.fm , prm.substr(1) ).value;
  176.  
  177.                     }
  178.  
  179.                     // "#myid"
  180.                     else if ( /^#\w+$/i.test(prm) ){
  181.  
  182.                         var tst = true;
  183.                         if ( $lm( prm.substr(1) ) )prm = $lm( prm.substr(1) ).value;
  184.  
  185.                     }
  186.  
  187.                 }
  188.  
  189.                 // if there's more than one input of the same class, verify each
  190.                 lm = $cc(this.fm,r.sbj);
  191.                 for (var i in lm){
  192.                  
  193.                     c = new chk(lm[i]);
  194.                     try{
  195.                      
  196.                         val = c.exec(r.fnc,prm);
  197.                         if (!val)val = r.msg;
  198.                         else val = "ok";
  199.                        
  200.                     }catch(er){
  201.                      
  202.                         val = er;
  203.                        
  204.                     }
  205.                    
  206.                     // uncomment below for debug
  207.                     //val +=  " (prm: " + prm + " , tst: " + tst + ")";
  208.                     this.res.push(val);
  209.                    
  210.                 }
  211.  
  212.             }
  213.  
  214.         }
  215.  
  216.     }
  217.  
  218.     // output
  219.     this.out = function(){
  220.  
  221.         if (this.res.length < 1)this.chk();
  222.  
  223.         var s = "";
  224.         for(var i = 0; i < this.res.length; i++ ){
  225.  
  226.             if(this.res[i] != true)s += this.res[i] + "<br>";
  227.  
  228.         }
  229.  
  230.         return s;
  231.  
  232.     }
  233.  
  234. }
  235.  
  236. function chk(obj){
  237.  
  238.     this.v = (obj.value) ? obj.value : obj.innerHTML;
  239.    
  240.     // numeric "equals"
  241.     this.eq = function(b){
  242.         if (pf(this.v) == pf(b))return true;
  243.         return false;
  244.     }
  245.  
  246.     // string equals ("exact")
  247.     this.ex = function(b){
  248.       if (this.v == b)return true;
  249.       return false;
  250.     }
  251.  
  252.     // numeric "lesser than"
  253.     this.lt = function(b){
  254.         if (pf(this.v) < pf(b))return true;
  255.         return false;
  256.     }
  257.  
  258.     // numeric "lesser or equals"
  259.     this.le = function(b){
  260.       if (pf(this.v) <= pf(b))return true;
  261.         return false;
  262.     }
  263.  
  264.     // numeric "greater than"
  265.     this.gt = function(b){
  266.       if (pf(this.v) > pf(b))return true;
  267.         return false;
  268.     }
  269.  
  270.     // numeric "greater or equals"
  271.     this.ge = function(b){
  272.       if (pf(this.v) >= pf(b))return true;
  273.         return false;
  274.     }
  275.  
  276.     // empty ?
  277.     this.nn = function(){
  278.         if (this.v.toString().length > 0)return true;
  279.         return false;
  280.     }
  281.  
  282.     // regexp pattern
  283.     this.rg = function(b){
  284.         var bb = new RegExp(b[0],b[1]);
  285.         return bb.test(this.v);
  286.     }
  287.  
  288.     this.exec = function(f, arg) {
  289.  
  290.         return this[f](arg);
  291.        
  292.     }
  293.  
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement