Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // for getElementById
- function $lm(obj){
- if ( document.getElementById(obj) ) return document.getElementById(obj);
- else return false;
- }
- // first child of the same 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;
- }
- // for firstChildOfClass
- function $fc(p, cl){
- return firstChildofClass(p,cl);
- }
- // checks if object has the defined class
- 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;
- }
- 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;
- }
- // for getChildElementsByClassName
- function $cc(obj,cl){
- return getChildElementsByClassName(obj,cl);
- }
- function pf(n){return parseFloat(n);}
- /* 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) link this script in your html head under some name (e.g. rule.js)
- * 2) insert the following script type="text/javascript" tag into your html head
- * 3) design your own rules
- * 4) design your own checker functions into the chk() class definition if necessary
- *
- * code into your html head:
- *
- window.onload = function(){
- // the form's send button input
- var sn = $lm("send");
- // the form dom object to be verified
- var fm = $lm("fm");
- // log div object to hold the verification result
- var lg = $lm("log");
- // send button event handler
- sn.onclick = function(){
- lg.innerHTML = "";
- var fc = new formChecker(fm,myRule);
- lg.innerHTML += fc.out();
- }
- }
- some rule examples:
- myRule = {
- first: {
- sbj: "nm",
- fnc: "nn",
- prm: "",
- msg: "Name fields can not be empty."
- },
- second: {
- sbj: "yr",
- fnc: "gt",
- prm: ".minyr",
- msg: "Must be over 18."
- },
- third: {
- sbj: "yr",
- fnc: "lt",
- prm: "#maxyr",
- msg: "Must be under 99."
- },
- fourth: {
- sbj: "cn",
- fnc: "nn",
- prm: "",
- msg: "Can not be empty."
- },
- fifth: {
- sbj: "cn",
- fnc: "rg",
- prm: ["^hu$","i"],
- msg: "Must be hungarian."
- },
- sixth: {
- sbj: "cn",
- fnc: "ex",
- prm: "hu",
- msg: "Must be hungarian."
- },
- seventh: {
- sbj: "cn",
- fnc: "dd",
- prm: "hu",
- msg: "Errorous rule (no chk object method "dd")."
- }
- }
- *
- **/
- function formChecker(fm,rule){
- if (!fm) return;
- this.fm = fm; // form DOM object
- this.rule = rule; // rule JSON object-list
- this.res = []; // result array
- this.chk = function(){
- var r,val,lm,c,prm;
- // 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 ( prm.match(/^\.\w+$/i) != null ){
- 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 in lm){
- c = new chk(lm[i]);
- try{
- val = c.exec(r.fnc,prm);
- if (!val)val = r.msg;
- else val = "ok";
- }catch(er){
- val = er;
- }
- // uncomment below for debug
- //val += " (prm: " + prm + " , tst: " + tst + ")";
- this.res.push(val);
- }
- }
- }
- }
- // output
- this.out = function(){
- if (this.res.length < 1)this.chk();
- var s = "";
- for(var i = 0; i < this.res.length; i++ ){
- if(this.res[i] != true)s += this.res[i] + "<br>";
- }
- return s;
- }
- }
- 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