/** Validator which supports regular expressions. */ function RegExValidator(sId, sRegEx, sErrorString, sErrorClass){ if ( sId ){ this._sId = sId; this._element = document.getElementById(sId); this._sOrginalClass = this._element.className; } this._sRegEx = sRegEx; this._sErrorString = sErrorString; this._sErrorClass = sErrorClass; } RegExValidator.prototype.setRegEx = function (sRegEx){ this._sRegEx = sRegEx; } RegExValidator.prototype.mark = function (bMark){ if ( typeof bMark == "undefined" || bMark == true ){ this._element.className = this._sErrorClass; } } RegExValidator.prototype.unmark = function (bMark){ if ( typeof bMark == "undefined" || bMark == true ) this._element.className = this._sOrginalClass; } /** Validates the element. @return[bool] The result of the validation */ RegExValidator.prototype.check = function (bMark){ if ( this._element && !this._element.disabled && !this._element.readOnly ) if (this._element.value.search(this._sRegEx) == -1 ){ this.mark(bMark); return false; } this.unmark(bMark); return true; } RegExValidator.prototype.getErrorString = function (){ return this._sErrorString; } RegExValidator.prototype.getId = function (){ return this._sId; } function TimeValidator(sId, sRegEx, sErrorString, sErrorClass){ RegExValidator.call(this, sId, sRegEx, sErrorString, sErrorClass); } TimeValidator.prototype = new RegExValidator(); function DateValidator(sId, sRegEx, sDelim, sFormat, sErrorString, sErrorClass){ RegExValidator.call(this, sId, sRegEx, sErrorString, sErrorClass); this._sDelim = sDelim; this._sFormat = sFormat; this._iMax; this._iMin; } DateValidator.prototype = new RegExValidator(); DateValidator.prototype.getMax = function(){ return this._iMax; } DateValidator.prototype.getMin = function(){ return this._iMin; } DateValidator.prototype.setMax = function(i){ this._iMax = i; } DateValidator.prototype.setMin = function(i){ this._iMin = i; } DateValidator.prototype.getTime = function(){ var sDelim = this._sDelim; if ( this._element.value.length < 1 ) return 0; aDate = this._element.value.split(sDelim); aFormat = this._sFormat.split(sDelim); d = new Date(); y=m=d=h=i=s = 0; for (j = 0; j < aDate.length; j++){ switch(aFormat[j].toLowerCase()){ case 'y': y = aDate[j]; break; case 'm': m = aDate[j]; break; case 'd': d = aDate[j]; break; case 'h': h = aDate[j]; break; case 'i': i = aDate[j]; break; case 's': s = aDate[j]; break; } } new_date = new Date(y,m-1,d,h,i,s); //alert(new_date.toLocaleString()); return new_date.getTime()/1000; } DateValidator.prototype.check = function(bMark){ if ( !RegExValidator.prototype.check.call(this, bMark) ) return false; if ( ( this.getMin() && (this.getMin() > this.getTime()) ) || ( this.getMax() && (this.getMax() < this.getTime()) ) ){ this.mark(bMark); return false; } this.unmark(bMark); return true; } function DatePeerValidator(sId, sPeerId, bPeerLesser, sRegEx, sDelim, sFormat, sErrorString, sErrorClass){ DateValidator.call(this, sId, sRegEx, sDelim, sFormat, sErrorString, sErrorClass); this._sPeerId = sPeerId; this._bPeerLesser = bPeerLesser; } DatePeerValidator.prototype = new DateValidator(); DatePeerValidator.prototype.setPeer = function(){ this._peer = eval("vali_"+this._sPeerId); } DatePeerValidator.prototype.compare = function(){ if ( this._bPeerLesser ) return this._peer.getTime() <= this.getTime(); else return this._peer.getTime() >= this.getTime(); } DatePeerValidator.prototype.check = function(bMark){ this.setPeer(); /* alert(this._sId); */ if ( DateValidator.prototype.check.call(this,bMark) && DateValidator.prototype.check.call(this._peer,false)){ if ( this._peer._element.value.length > 0 && !this.compare() && this._element.value.length > 0) this.mark(bMark); else{ this.unmark(bMark); return true; } } return false; } function DatePairValidator(sIdA, sIdB, sRegEx, sDelim, sFormat, sErrorString, sErrorClass){ this._ValiA = new DatePeerValidator(sIdA, false, sRegEx, sDelim, sFormat, sErrorString, sErrorClass); this._ValiB = new DatePeerValidator(sIdB, true, sRegEx, sDelim, sFormat, sErrorString, sErrorClass); } DatePairValidator.prototype.first = function(){ return this._ValiA; } DatePairValidator.prototype.second = function(){ return this._ValiB;} /*DatePairValidator.prototype.check = function(){ return this._ValiA.check() && this._ValiB.check(); }*/ function Validator(){ var aValis = new Array(); var _ErrCallback = null; /** Adds an item to be validated @param item The elements id @param regex The regular expression the item should be checked against @return NULL */ this.addItem = function (vali){ aValis[aValis.length]= vali; } this.setErrorCallback= function (f) { _ErrCallback = f; } /** Validate all Elements that should be checked. @return[bool] The result of the validation */ this.validate = function(){ var b = true; var e = ""; var label = ""; var error = ""; if(_ErrCallback) { // If there is a registered callback, we collect the error information // and pass it to the callback var aErr = new Array(); for(var i = 0; i < aValis.length; i++){ var vali = aValis[i]; if ( !vali.check() ){ b = false; if ( (o = document.getElementById("label_"+vali.getId())) ) label = o.innerHTML; else if ( ( o = document.getElementById(vali.getId()) ) ) label = o.value; if ( (error = vali.getErrorString()).length ) e = error; else e = "Invalid input\n"; aErr.push([label,e]); } } if(aErr.length > 0) _ErrCallback(aErr); } else { for(var i = 0; i < aValis.length; i++){ var vali = aValis[i]; if ( !vali.check() ){ b = false; if ( (o = document.getElementById("label_"+vali.getId())) ) label = o.innerHTML; else if ( ( o = document.getElementById(vali.getId()) ) ) label = o.value; e = e + label + "\n"; if ( (error = vali.getErrorString()).length ) e = e + error+"\n"; else e = e + "Invalid input\n"; } } if ( e ) alert(e); } return b; } } vali=new Validator();