FormValidator = function(oForm) { var _aValidations = []; var _oMessage = null; var _errDisplayId = ""; var _defaultErrMsgId = ""; var _errMsgId = ""; function _init() { YAHOO.util.Event.addListener(oForm, 'submit', _onFormSubmit); } function _onFormSubmit(oEvent) { var bHasErrors = false; for (var n=0; n<_aValidations.length; n++) { if(_aValidations[n].element){ _validate( _aValidations[n].element, _aValidations[n].validation, _aValidations[n].mandatory, _aValidations[n].sMessage, _aValidations[n].defaultErrId ); //console.log(_hasError(_aValidations[n].element, _aValidations[n].validation, _aValidations[n].defaultErrId)); if (_hasError(_aValidations[n].element, _aValidations[n].validation, _aValidations[n].defaultErrId)) { //console.log("has error"); bHasErrors = true; } } } if (bHasErrors) { //_showError(null, 'This form contains erros, please try again'); YAHOO.util.Event.preventDefault(oEvent); } //YAHOO.util.Event.preventDefault(oEvent); } function _hasError(oElement, oValidation, defaultErrId) { if(oElement && oValidation){ if(defaultErrId == null){ _defaultErrMsgId = oElement.name+"_defaultErrMsg"; _errMsgId = oElement.name+"_errMsg"; } else{ _defaultErrMsgId = defaultErrId+"_defaultErrMsg"; _errMsgId = defaultErrId+"_errMsg"; } if(document.getElementById(_defaultErrMsgId) || document.getElementById(_errMsgId)) return (YAHOO.util.Dom.hasClass(_defaultErrMsgId, 'showInlineErrorMsg') || YAHOO.util.Dom.hasClass(_errMsgId, 'showInlineErrorMsg')); } } function _validate(oElement, oValidation, bMandatory, sMessage, defaultErrId) { oElement.value = FormValidator.String.removeExtraSpaces(oElement.value); var sValue = FormValidator.String.trim(oElement.value); if (bMandatory) { if (sValue == '' || sValue == '--' || sValue == '-1' || sValue == 'XX') { _showError(oElement, oValidation, sMessage, defaultErrId); return; } else _hideError(oElement, defaultErrId); } else { if (sValue == '' || sValue == '--' || sValue == '-1' || sValue == 'XX') { _showValid(oElement, sMessage, defaultErrId); return; } } if (!oValidation || !oValidation['check']) { return; } //console.log("@ Chk :"+oValidation['check'](oElement.value)+"-----------------------------"+(!oValidation['check'](oElement.value))); if (oElement.value && !oValidation['check'](oElement.value)){ //console.log("@ IF :"+oValidation['check'](oElement.value)); _showError(oElement, oValidation, sMessage, defaultErrId); } else{ if (oValidation['format']) oElement.value = oValidation['format'](oElement.value); _showValid(oElement, sMessage, defaultErrId); } } function _showError(oElement, oValidation, sMessage, defaultErrId) { if (oElement){ if(defaultErrId == null){ _defaultErrMsgId = oElement.name+"_defaultErrMsg"; _errMsgId = oElement.name+"_errMsg"; } else{ _defaultErrMsgId = defaultErrId+"_defaultErrMsg"; _errMsgId = defaultErrId+"_errMsg"; } //console.log("@ Err : "+oValidation['check'](oElement.value)+"\n\n"+oElement.name); if(oElement.value == "" || oElement.value == "--" || oElement.value == "-1" || oElement.value == "XX"){ _errDisplayId = _defaultErrMsgId; if(document.getElementById(_errMsgId)) YAHOO.util.Dom.removeClass(_errMsgId, 'showInlineErrorMsg'); } else if(oElement.value && !oValidation['check'](oElement.value)){ _errDisplayId = _errMsgId; if(document.getElementById(_defaultErrMsgId)) YAHOO.util.Dom.removeClass(_defaultErrMsgId, 'showInlineErrorMsg'); } //console.log("Chk Val : "+oValidation['check'](oElement.value)+"-----"+_errDisplayId); if(document.getElementById(_errDisplayId) && !oValidation['check'](oElement.value)) YAHOO.util.Dom.addClass(_errDisplayId, 'showInlineErrorMsg'); else YAHOO.util.Dom.removeClass(_errDisplayId, 'showInlineErrorMsg'); } if(sMessage != "" && sMessage != undefined && sMessage != null){ if(oElement) if(document.getElementById(_errDisplayId)) document.getElementById(_errDisplayId).innerHTML = sMessage; } // _oMessage.innerHTML = 'This form contains erros, please try again'; //YAHOO.util.Dom.setStyle(_oMessage, 'display', 'block'); } function _showValid(oElement, sMessage, defaultErrId) { if (oElement) { if(defaultErrId == null){ _defaultErrMsgId = oElement.name+"_defaultErrMsg"; _errMsgId = oElement.name+"_errMsg"; } else{ _defaultErrMsgId = defaultErrId+"_defaultErrMsg"; _errMsgId = defaultErrId+"_errMsg"; } if(document.getElementById(_defaultErrMsgId) || document.getElementById(_errMsgId)) return (YAHOO.util.Dom.removeClass(_defaultErrMsgId, 'showInlineErrorMsg') || YAHOO.util.Dom.removeClass(_errMsgId, 'showInlineErrorMsg')); } } function _hideError(oElement, defaultErrId) { if(oElement){ if(defaultErrId == null){ _defaultErrMsgId = oElement.name+"_defaultErrMsg"; _errMsgId = oElement.name+"_errMsg"; } else{ _defaultErrMsgId = defaultErrId+"_defaultErrMsg"; _errMsgId = defaultErrId+"_errMsg"; } //YAHOO.util.Dom.setStyle(_oMessage, 'display', 'none'); if(document.getElementById(_defaultErrMsgId) || document.getElementById(_errMsgId)) return (YAHOO.util.Dom.removeClass(_defaultErrMsgId, 'showInlineErrorMsg') || YAHOO.util.Dom.removeClass(_errMsgId, 'showInlineErrorMsg')); } } this.addValidation = function(sFieldName, oValidation, bMandatory, sMessage,defaultErrId) { var oElement = oForm[sFieldName]; if (typeof sMessage == 'function') { sMessage = sMessage(oElement.value); } _aValidations.push({ element : oElement, validation : oValidation, mandatory : bMandatory, message : sMessage, defaultErrId: defaultErrId }); if(oElement) if (typeof oElement.value == 'undefined') { return; } YAHOO.util.Event.addListener(oElement, 'blur', function() { if(oElement) _validate(oElement, oValidation, bMandatory, sMessage, defaultErrId); }); if(oElement){ if (oElement.tagName.toLowerCase() == 'select') { YAHOO.util.Event.addListener(oElement, 'change', function() { _validate(oElement, oValidation, bMandatory, sMessage, defaultErrId); }); } } YAHOO.util.Event.addListener(oElement, 'keyup', function() { if(_hasError(oElement, oValidation, defaultErrId) && oElement) { _validate(oElement, oValidation, bMandatory, sMessage, defaultErrId); } }); }; _init(); }; FormValidator.String = { trim: function(s) { return s.replace(/^[\s]*|[\s]*$/g, ''); }, removeExtraSpaces: function(s) { return s.replace(/^[\s]*|[\s]*$/g, '').replace(/[\s]+/g, ' '); }, ucWords: function(s) { var aWords = s.split(/[\s]/g); var aCWords = []; for (var n=0; n