Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // shorthand for getElementById
- function $lm(obj){
- if ( document.getElementById(obj) ) return document.getElementById(obj);
- else return false;
- }
- // object's first child with the given class
- function firstChildofClass(p,cl) {
- if (!p || !p.childNodes.length)return;
- var ch = p.childNodes;
- for ( var i = 0; i < ch.length; i++ ){
- if (gotClass(ch[i],cl))return ch[i];
- }
- return;
- }
- // shorthand for firstChildOfClass
- function $fc(p, cl){
- return firstChildofClass(p,cl);
- }
- function gotClass(obj,cl) {
- var r = new RegExp("\\b" + cl + "\\b", "i");
- var ob = ( typeof(obj) == "object" ) ? obj : $lm(obj);
- if ( ob && ob.className && ob.className.match(r) !== null ) return true;
- return false;
- }
- // object's all children with given class
- function getChildElementsByClassName(obj,cl){
- var oc = ( obj != undefined ) ? obj.childNodes : document.body.childNodes;
- var ch = [];
- for ( var i = 0; i < oc.length; i++ ){
- if (gotClass(oc[i],cl)) ch.push(oc[i]);
- }
- return ch;
- }
- // shorthand for getChildElementsByClassName
- function $cc(obj,cl){
- return getChildElementsByClassName(obj,cl);
- }
- // shorthand for parseFloat
- function pf(n){return parseFloat(n);}
- // set object's css style
- function setStyle(obj,st,vl){
- var a = obj.style.cssText;
- obj.style.cssText = (a.length>0) ? a + ";" + st + ":" + vl : st + ":" + vl ;
- }
- // trim and compress spaces
- String.prototype.clean = function(){
- return this.replace(/^(\s+)/g,"").replace(/(\s+)$/g , "").replace(/\s\s+/g , " ");
- }
- // add given class to object's className
- function addClass(obj,c){
- obj.className = (obj.className.length > 0) ? obj.className + " " + c : c;
- }
- // remove given class from object's className
- function removeClass(obj,c){
- var r = new RegExp("(\\b" + c + "\\b)", "gi");
- obj.className = obj.className.replace( r , " " ).clean();
- }
- /* formChecker class definition
- * parameters:
- * fm - the form to be verified
- * rule - form rules in JSON format
- * rule record (object) properties:
- * sbj - input class
- * if the form contains more than one input of the same class, each one will be verified
- * fnc - verifier function (these are methods of the chk() object class)
- * see functions for more details
- * prm - value to verify against
- * can be a fixed value or a form elem value defined by class or id (css-like definitions -> ".myclass" or "#myid")
- *
- * usage:
- * 1) design your own checker functions into chk() class definition if necessary
- * 2) design your own rules
- * 3) design callback functions (init, success, failure, error)
- * 4) insert init code
- *
- * for live example see my pastebin
- *
- **/
- function formChecker(fm,rule,cf,sf,ff,ef){
- if (!fm) return;
- this.fm = fm; // form DOM object
- this.rule = rule; // rule JSON object-list
- this.res = []; // result array
- this.nof = 0; // number of fails
- if ( cf == undefined || typeof cf != "function" )cf = null;
- if ( sf == undefined || typeof sf != "function" )sf = null;
- if ( ff == undefined || typeof ff != "function" )ff = null;
- if ( ef == undefined || typeof ef != "function" )ef = null;
- this.chk = function(){
- var r,val,lm,c,prm;
- // clear earlier results
- cf(this.fm);
- // rules
- for (var i in this.rule){
- r = this.rule[i];
- // check only if object exists
- if ( $fc(this.fm,r.sbj) ){
- // handle parameters
- // only try to find css-like object reference, if not number and not regexp
- // css-style DOM reference lookup with ".myclass" and "#myid"
- prm = r.prm;
- if ( isNaN(prm) && r.fnc != "rg" ){
- // ".myclass"
- if ( /^\.\w+$/i.test(prm) ){
- var tst = true;
- // only first object of the same class
- if ( $fc( this.fm , prm.substr(1) ) )prm = $fc( this.fm , prm.substr(1) ).value;
- }
- // "#myid"
- else if ( /^#\w+$/i.test(prm) ){
- var tst = true;
- if ( $lm( prm.substr(1) ) )prm = $lm( prm.substr(1) ).value;
- }
- }
- // if there's more than one input of the same class, verify each
- lm = $cc(this.fm,r.sbj);
- for (var i = 0; i < lm.length; i++){
- c = new chk(lm[i]);
- try{
- val = c.exec(r.fnc,prm);
- // fail
- if(!val){
- ff(lm[i],r.msg);
- this.nof++;
- }
- // success
- else sf(lm[i],"");
- } catch(er) {
- // error
- val = ef(er);
- }
- this.res.push(val);
- }
- }
- }
- return this.nof;
- }
- }
- function chk(obj){
- this.v = (obj.value) ? obj.value : obj.innerHTML;
- // numeric "equals"
- this.eq = function(b){
- if (pf(this.v) == pf(b))return true;
- return false;
- }
- // string equals ("exact")
- this.ex = function(b){
- if (this.v == b)return true;
- return false;
- }
- // numeric "lesser than"
- this.lt = function(b){
- if (pf(this.v) < pf(b))return true;
- return false;
- }
- // numeric "lesser or equals"
- this.le = function(b){
- if (pf(this.v) <= pf(b))return true;
- return false;
- }
- // numeric "greater than"
- this.gt = function(b){
- if (pf(this.v) > pf(b))return true;
- return false;
- }
- // numeric "greater or equals"
- this.ge = function(b){
- if (pf(this.v) >= pf(b))return true;
- return false;
- }
- // empty ?
- this.nn = function(){
- if (this.v.toString().length > 0)return true;
- return false;
- }
- // regexp pattern
- this.rg = function(b){
- var bb = new RegExp(b[0],b[1]);
- return bb.test(this.v);
- }
- this.exec = function(f, arg) {
- return this[f](arg);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement