Advertisement
jeges

formChecker object class v0.2

Aug 1st, 2011
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // shorthand for getElementById
  3. function $lm(obj){
  4.   if ( document.getElementById(obj) ) return document.getElementById(obj);
  5.   else return false;
  6. }
  7.  
  8. // object's first child with the given class
  9. function firstChildofClass(p,cl) {
  10.   if (!p || !p.childNodes.length)return;
  11.   var ch = p.childNodes;
  12.   for ( var i = 0; i < ch.length; i++ ){
  13.     if (gotClass(ch[i],cl))return ch[i];
  14.   }
  15.   return;
  16. }
  17.  
  18. // shorthand for firstChildOfClass
  19. function $fc(p, cl){
  20.   return firstChildofClass(p,cl);
  21. }
  22.  
  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. // object's all children with given class
  31. function getChildElementsByClassName(obj,cl){
  32.   var oc = ( obj != undefined ) ? obj.childNodes : document.body.childNodes;
  33.   var ch = [];
  34.   for ( var i = 0; i < oc.length; i++ ){
  35.     if (gotClass(oc[i],cl)) ch.push(oc[i]);
  36.   }
  37.   return ch;
  38. }
  39.  
  40. // shorthand for getChildElementsByClassName
  41. function $cc(obj,cl){
  42.   return getChildElementsByClassName(obj,cl);
  43. }
  44.  
  45. // shorthand for parseFloat
  46. function pf(n){return parseFloat(n);}
  47.  
  48. // set object's css style
  49. function setStyle(obj,st,vl){
  50.     var a = obj.style.cssText;
  51.     obj.style.cssText = (a.length>0) ? a + ";" + st + ":" + vl : st + ":" + vl ;
  52. }
  53.  
  54. // trim and compress spaces
  55. String.prototype.clean = function(){
  56.     return this.replace(/^(\s+)/g,"").replace(/(\s+)$/g , "").replace(/\s\s+/g , " ");
  57. }
  58.  
  59. // add given class to object's className
  60. function addClass(obj,c){
  61.     obj.className = (obj.className.length > 0) ? obj.className + " " + c : c;
  62. }
  63.  
  64. // remove given class from object's className
  65. function removeClass(obj,c){
  66.     var r = new RegExp("(\\b" + c + "\\b)", "gi");
  67.     obj.className = obj.className.replace( r , " " ).clean();
  68. }
  69.  
  70. /* formChecker class definition
  71.  * parameters:
  72.  * fm - the form to be verified
  73.  * rule - form rules in JSON format
  74.  * rule record (object) properties:
  75.  * sbj - input class
  76.  *      if the form contains more than one input of the same class, each one will be verified
  77.  * fnc - verifier function (these are methods of the chk() object class)
  78.  *      see functions for more details
  79.  * prm - value to verify against
  80.  *      can be a fixed value or a form elem value defined by class or id (css-like definitions -> ".myclass" or "#myid")
  81.  *
  82.  * usage:
  83.  *      1) design your own checker functions into chk() class definition if necessary
  84.  *      2) design your own rules
  85.  *      3) design callback functions (init, success, failure, error)
  86.  *      4) insert init code
  87.  *
  88.  *      for live example see my pastebin
  89.  *
  90. **/
  91.  
  92. function formChecker(fm,rule,cf,sf,ff,ef){
  93.  
  94.     if (!fm) return;
  95.  
  96.     this.fm = fm;       // form DOM object
  97.     this.rule = rule;   // rule JSON object-list
  98.     this.res = [];      // result array
  99.     this.nof = 0;       // number of fails
  100.  
  101.     if ( cf == undefined || typeof cf != "function" )cf = null;
  102.     if ( sf == undefined || typeof sf != "function" )sf = null;
  103.     if ( ff == undefined || typeof ff != "function" )ff = null;
  104.     if ( ef == undefined || typeof ef != "function" )ef = null;
  105.  
  106.     this.chk = function(){
  107.  
  108.         var r,val,lm,c,prm;
  109.  
  110.         // clear earlier results
  111.         cf(this.fm);
  112.  
  113.         // rules
  114.         for (var i in this.rule){
  115.  
  116.             r = this.rule[i];
  117.  
  118.             // check only if object exists
  119.             if ( $fc(this.fm,r.sbj) ){
  120.  
  121.                 // handle parameters
  122.                 // only try to find css-like object reference, if not number and not regexp
  123.                 // css-style DOM reference lookup with ".myclass" and "#myid"
  124.  
  125.                 prm = r.prm;
  126.  
  127.                 if ( isNaN(prm) && r.fnc != "rg" ){
  128.  
  129.                     // ".myclass"
  130.                     if ( /^\.\w+$/i.test(prm) ){
  131.  
  132.                         var tst = true;
  133.                         // only first object of the same class
  134.                         if ( $fc( this.fm , prm.substr(1) ) )prm = $fc( this.fm , prm.substr(1) ).value;
  135.  
  136.                     }
  137.  
  138.                     // "#myid"
  139.                     else if ( /^#\w+$/i.test(prm) ){
  140.  
  141.                         var tst = true;
  142.                         if ( $lm( prm.substr(1) ) )prm = $lm( prm.substr(1) ).value;
  143.  
  144.                     }
  145.  
  146.                 }
  147.  
  148.                 // if there's more than one input of the same class, verify each
  149.                 lm = $cc(this.fm,r.sbj);
  150.                 for (var i = 0; i < lm.length; i++){
  151.  
  152.                     c = new chk(lm[i]);
  153.                     try{
  154.  
  155.                         val = c.exec(r.fnc,prm);
  156.  
  157.                         // fail
  158.                         if(!val){
  159.  
  160.                             ff(lm[i],r.msg);
  161.                             this.nof++;
  162.  
  163.                         }
  164.  
  165.                         // success
  166.                         else sf(lm[i],"");
  167.  
  168.                     } catch(er) {
  169.  
  170.                         // error
  171.                         val = ef(er);
  172.  
  173.                     }
  174.  
  175.                     this.res.push(val);
  176.  
  177.                 }
  178.  
  179.             }
  180.  
  181.         }
  182.  
  183.         return this.nof;
  184.  
  185.     }
  186.  
  187. }
  188.  
  189. function chk(obj){
  190.  
  191.     this.v = (obj.value) ? obj.value : obj.innerHTML;
  192.  
  193.     // numeric "equals"
  194.     this.eq = function(b){
  195.         if (pf(this.v) == pf(b))return true;
  196.         return false;
  197.     }
  198.  
  199.     // string equals ("exact")
  200.     this.ex = function(b){
  201.       if (this.v == b)return true;
  202.       return false;
  203.     }
  204.  
  205.     // numeric "lesser than"
  206.     this.lt = function(b){
  207.         if (pf(this.v) < pf(b))return true;
  208.         return false;
  209.     }
  210.  
  211.     // numeric "lesser or equals"
  212.     this.le = function(b){
  213.       if (pf(this.v) <= pf(b))return true;
  214.         return false;
  215.     }
  216.  
  217.     // numeric "greater than"
  218.     this.gt = function(b){
  219.       if (pf(this.v) > pf(b))return true;
  220.         return false;
  221.     }
  222.  
  223.     // numeric "greater or equals"
  224.     this.ge = function(b){
  225.       if (pf(this.v) >= pf(b))return true;
  226.         return false;
  227.     }
  228.  
  229.     // empty ?
  230.     this.nn = function(){
  231.         if (this.v.toString().length > 0)return true;
  232.         return false;
  233.     }
  234.  
  235.     // regexp pattern
  236.     this.rg = function(b){
  237.         var bb = new RegExp(b[0],b[1]);
  238.         return bb.test(this.v);
  239.     }
  240.  
  241.     this.exec = function(f, arg) {
  242.  
  243.         return this[f](arg);
  244.  
  245.     }
  246.  
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement