/*********************************************************************** * YAV - Yet Another Validator v1.3.0 * * Copyright (C) 2005-2006 * * Author: Federico Crivellaro * * WWW: http://yav.sourceforge.net * * * * This library is free software; you can redistribute it and/or * * modify it under the terms of the GNU Lesser General Public * * License as published by the Free Software Foundation; either * * version 2.1 of the License, or (at your option) any later version. * * * * This library is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * * Lesser General Public License for more details. * * * * You should have received a copy of the GNU Lesser General Public * * License along with this library; if not, write to the Free Software * * Foundation, Inc.,59 Temple Place,Suite 330,Boston,MA 02111-1307 USA * * * * last revision: 21 SEP 2006 * ***********************************************************************/ //------------------------------------------------------------ PUBLIC FUNCTIONS var undef; var internalRules; function performCheck(formName, strRules, alertType) { var rules = makeRules(strRules); internalRules = makeRules(strRules); this.f = document.forms[formName]; if( !this.f ) { debug('DEBUG: could not find form object ' + formName); return null; } var errors = new Array(); var ix = 0; if (rules.length) { for(var i=0; i0 ) { if (strTrim(HEADER_MSG).length > 0) { str += HEADER_MSG + '\n\n'; } for (var i=0; i 0) { str += '\n' + FOOTER_MSG; } alert(str); return false; } else { return true; } } function displayInnerHtml(messages) { if ( messages!=null && messages.length>0 ) { var str = ''; if (strTrim(HEADER_MSG).length > 0) { str += HEADER_MSG; } str += '
    '; for (var i=0; i'; } str += '
'; if (strTrim(FOOTER_MSG).length > 0) { str += FOOTER_MSG; } document.getElementById(errorsdiv).innerHTML = str; document.getElementById(errorsdiv).className = innererror; document.getElementById(errorsdiv).style.display = 'block'; return false; } else { document.getElementById(errorsdiv).innerHTML = ''; document.getElementById(errorsdiv).className = ''; document.getElementById(errorsdiv).style.display = 'none'; return true; } } function displayJsVar(messages) { document.getElementById(errorsdiv).className = ''; document.getElementById(errorsdiv).style.display = 'none'; if ( messages!=null && messages.length>0 ) { var str = ''; str += ''; document.getElementById(errorsdiv).innerHTML = str; jsErrors = messages; return false; } else { document.getElementById(errorsdiv).innerHTML = ''; return true; } } function rule(el, ruleName, comparisonValue, alertMsg, ruleType) { if ( !checkArguments(arguments) ) { return false; } tmp = el.split(':'); nameDisplayed = ''; if (tmp.length == 2) { nameDisplayed = tmp[1]; el = tmp[0]; } this.el = el; this.nameDisplayed = nameDisplayed; this.ruleName = ruleName; this.comparisonValue = comparisonValue; this.ruleType = ruleType; if (alertMsg==undef || alertMsg==null) { this.alertMsg = getDefaultMessage(el, nameDisplayed, ruleName, comparisonValue); } else { this.alertMsg = alertMsg; } } function checkRule(f, myRule) { retVal = null; if (myRule != null) { if (myRule.ruleName=='custom') { var customFunction = ' retVal = ' + myRule.el; eval(customFunction); } else if (myRule.ruleName=='and') { var op_1 = myRule.el; var op_next = myRule.comparisonValue; if ( checkRule(f, internalRules[op_1])!=null ) { retVal = myRule.alertMsg; if (myRule.ruleType=='pre-condition') { highlight(getField(f, internalRules[op_1].el), inputclassnormal); } } else { op_k = op_next.split('-'); for(var k=0; k myRule.comparisonValue ) { highlight(el, inputclasserror); err = myRule.alertMsg; } } else if (myRule.ruleName=='minlength') { if ( isNaN(myRule.comparisonValue) ) { debug('DEBUG: comparisonValue for rule ' + myRule.ruleName + ' not a number'); } else if ( el.value.length < myRule.comparisonValue ) { highlight(el, inputclasserror); err = myRule.alertMsg; } } else if (myRule.ruleName=='numrange') { reg = new RegExp("^[-+]{0,1}[0-9]*[.]{0,1}[0-9]*$"); if ( !reg.test(unformatNumber(el.value)) ) { highlight(el, inputclasserror); err = myRule.alertMsg; } else { regRange = new RegExp("^[0-9]+-[0-9]+$"); if ( !regRange.test(myRule.comparisonValue) ) { debug('DEBUG: comparisonValue for rule ' + myRule.ruleName + ' not in format number1-number2'); } else { rangeVal = myRule.comparisonValue.split('-'); if (eval(unformatNumber(el.value))eval(rangeVal[1])) { highlight(el, inputclasserror); err = myRule.alertMsg; } } } } else if (myRule.ruleName=='regexp') { reg = new RegExp(myRule.comparisonValue); if ( !reg.test(el.value) ) { highlight(el, inputclasserror); err = myRule.alertMsg; } } else if (myRule.ruleName=='integer') { err = checkInteger(el, myRule); } else if (myRule.ruleName=='double') { err = checkDouble(el, myRule); } else if (myRule.ruleName=='date') { err = checkDate(el, myRule); } else if (myRule.ruleName=='date_lt') { err = checkDateLessThan(el, myRule, false); } else if (myRule.ruleName=='date_le') { err = checkDateLessThan(el, myRule, true); } else if (myRule.ruleName=='keypress') { // do nothing } else if (myRule.ruleName=='empty') { if ( el.value!=null && el.value!='' ) { highlight(el, inputclasserror); err = myRule.alertMsg; } } else { debug('DEBUG: rule ' + myRule.ruleName + ' not supported for ' + el.type); } return err; } function checkInteger(el, myRule) { reg = new RegExp("^[-+]{0,1}[0-9]*$"); if ( !reg.test(el.value) ) { highlight(el, inputclasserror); return myRule.alertMsg; } } function checkDouble(el, myRule) { var sep = DECIMAL_SEP; reg = new RegExp("^[-+]{0,1}[0-9]*[" + sep + "]{0,1}[0-9]*$"); if ( !reg.test(el.value) ) { highlight(el, inputclasserror); return myRule.alertMsg; } } function checkDate(el, myRule) { error = null; if (el.value!='') { var dateFormat = DATE_FORMAT; ddReg = new RegExp("dd"); MMReg = new RegExp("MM"); yyyyReg = new RegExp("yyyy"); if ( !ddReg.test(dateFormat) || !MMReg.test(dateFormat) || !yyyyReg.test(dateFormat) ) { debug('DEBUG: locale format ' + dateFormat + ' not supported'); } else { ddStart = dateFormat.indexOf('dd'); MMStart = dateFormat.indexOf('MM'); yyyyStart = dateFormat.indexOf('yyyy'); } strReg = dateFormat.replace('dd','[0-9]{2}').replace('MM','[0-9]{2}').replace('yyyy','[0-9]{4}'); reg = new RegExp("^" + strReg + "$"); if ( !reg.test(el.value) ) { highlight(el, inputclasserror); error = myRule.alertMsg; } else { dd = el.value.substring(ddStart, ddStart+2); MM = el.value.substring(MMStart, MMStart+2); yyyy = el.value.substring(yyyyStart, yyyyStart+4); if ( !checkddMMyyyy(dd, MM, yyyy) ) { highlight(el, inputclasserror); error = myRule.alertMsg; } } } return error; } function checkDateLessThan(el, myRule, isEqualAllowed) { error = null; var isDate = checkDate(el, myRule)==null ? true : false; if ( isDate && el.value!='' ) { var dateFormat = DATE_FORMAT; ddStart = dateFormat.indexOf('dd'); MMStart = dateFormat.indexOf('MM'); yyyyStart = dateFormat.indexOf('yyyy'); dd = el.value.substring(ddStart, ddStart+2); MM = el.value.substring(MMStart, MMStart+2); yyyy = el.value.substring(yyyyStart, yyyyStart+4); myDate = "" + yyyy + MM + dd; strReg = dateFormat.replace('dd','[0-9]{2}').replace('MM','[0-9]{2}').replace('yyyy','[0-9]{4}'); reg = new RegExp("^" + strReg + "$"); var isMeta = myRule.comparisonValue.indexOf('$')==0 ? true : false; var comparisonDate = ''; if (isMeta) { toSplit = myRule.comparisonValue.substr(1); tmp = toSplit.split(':'); if (tmp.length == 2) { comparisonDate = this.getField(f, tmp[0]).value; } else { comparisonDate = this.getField(f, myRule.comparisonValue.substr(1)).value; } } else { comparisonDate = myRule.comparisonValue; } if ( !reg.test(comparisonDate) ) { highlight(el, inputclasserror); error = myRule.alertMsg; } else { cdd = comparisonDate.substring(ddStart, ddStart+2); cMM = comparisonDate.substring(MMStart, MMStart+2); cyyyy = comparisonDate.substring(yyyyStart, yyyyStart+4); cDate = "" + cyyyy + cMM + cdd; if (isEqualAllowed) { if ( !checkddMMyyyy(cdd, cMM, cyyyy) || myDate>cDate ) { highlight(el, inputclasserror); error = myRule.alertMsg; } } else { if ( !checkddMMyyyy(cdd, cMM, cyyyy) || myDate>=cDate ) { highlight(el, inputclasserror); error = myRule.alertMsg; } } } } else { if ( el.value!='' ) { highlight(el, inputclasserror); error = myRule.alertMsg; } } return error; } function checkEqual(el, myRule) { error = null; var isMeta = myRule.comparisonValue.indexOf('$')==0 ? true : false; var comparisonVal = ''; if (isMeta) { toSplit = myRule.comparisonValue.substr(1); tmp = toSplit.split(':'); if (tmp.length == 2) { comparisonVal = this.getField(f, tmp[0]).value; } else { comparisonVal = this.getField(f, myRule.comparisonValue.substr(1)).value; } } else { comparisonVal = myRule.comparisonValue; } if ( el.value!=comparisonVal ) { highlight(el, inputclasserror); error = myRule.alertMsg; } return error; } function checkNotEqual(el, myRule) { error = null; var isMeta = myRule.comparisonValue.indexOf('$')==0 ? true : false; var comparisonVal = ''; if (isMeta) { toSplit = myRule.comparisonValue.substr(1); tmp = toSplit.split(':'); if (tmp.length == 2) { comparisonVal = this.getField(f, tmp[0]).value; } else { comparisonVal = this.getField(f, myRule.comparisonValue.substr(1)).value; } } else { comparisonVal = myRule.comparisonValue; } if ( el.value==comparisonVal ) { highlight(el, inputclasserror); error = myRule.alertMsg; } return error; } function checkddMMyyyy(dd, MM, yyyy) { retVal = true; if ( (dd > 31) || (MM > 12) || (dd==31 && (MM==2 || MM==4 || MM==6 || MM==9 || MM==11) ) || (dd >29 && MM==2) || (dd==29 && (MM==2) && ((yyyy%4 > 0) || (yyyy%4==0 && yyyy%100==0 && yyyy%400>0 )) )) { retVal = false; } return retVal; } function checkCheckbox(el, myRule) { if (myRule.ruleName=='required') { if ( !el.checked ) { highlight(el, inputclasserror); return myRule.alertMsg; } } else if (myRule.ruleName=='equal') { if ( !el.checked || el.value!=myRule.comparisonValue ) { highlight(el, inputclasserror); return myRule.alertMsg; } } else if (myRule.ruleName=='notequal') { if ( !el.checked || el.value==myRule.comparisonValue ) { highlight(el, inputclasserror); return myRule.alertMsg; } } else { debug('DEBUG: rule ' + myRule.ruleName + ' not supported for ' + el.type); } } function checkSelOne(el, myRule) { if (myRule.ruleName=='required') { var found = false; var inx = el.selectedIndex; if(inx>=0 && el.options[inx].value) { found = true; } if ( !found ) { highlight(el, inputclasserror); return myRule.alertMsg; } } else if (myRule.ruleName=='equal') { var found = false; var inx = el.selectedIndex; if(inx>=0 && el.options[inx].value==myRule.comparisonValue) { found = true; } if ( !found ) { highlight(el, inputclasserror); return myRule.alertMsg; } } else if (myRule.ruleName=='notequal') { var found = false; var inx = el.selectedIndex; if(inx>=0 && el.options[inx].value!=myRule.comparisonValue) { found = true; } if ( !found ) { highlight(el, inputclasserror); return myRule.alertMsg; } } else { debug('DEBUG: rule ' + myRule.ruleName + ' not supported for ' + el.type); } } function checkSelMul(el, myRule) { if (myRule.ruleName=='required') { var found = false; opts = el.options; for(var i=0; i