/***** SOPORTE PARA VALIDACION *****/ function Validate(oControl) { var SinErrores = true; //Función principal de validación - Se validan requeridos, int, decimal, fecha, máx y min var control = document.getElementById(oControl); if (RequeridosHabilitados == true && control.getAttribute('requerido') == 'true' && ( control.value == '' || (control.getAttribute('TipoInt') == 'true' && EsEntero(control.value) && parseInt(control.value) == 0 ) || (control.getAttribute('TipoDecimal') == 'true' && !isNaN(control.value) /*&& parseFloat(control.value) == 0*/ ) ) ) { MostrarAlerta(oControl, msgREQUERIDO);SinErrores = false; } else if(control.getAttribute('TipoInt') == 'true' && control.value != '') { if(!ValidarEntero(control)){MostrarAlerta(oControl, msgENTERO_INVALIDO);SinErrores = false;} else if(!ValidarRangoEntero(control)){ var strRango = VerbalizarRango(control); MostrarAlerta(oControl, msgFUERA_DE_RANGO + " (" + strRango + ")");SinErrores = false;} } else if(control.getAttribute('TipoDecimal') == 'true' && control.value != '') { if(!ValidarDecimal(control)){MostrarAlerta(oControl, msgDECIMAL_INVALIDO);SinErrores = false;} else if(!ValidarRangoDecimal(control)){ var strRango = VerbalizarRango(control); MostrarAlerta(oControl, msgFUERA_DE_RANGO + " (" + strRango + ")");SinErrores = false;} } else if (control.getAttribute('TipoFechaHora') == 'true'&& control.value != '') { //FECHA +HORA!! if (!ValidarFechaHora(control)) { MostrarAlerta(oControl, msgFECHAHORA_INVALIDA); return false; } else if (!ValidarRangoFecha(control)) { MostrarAlerta(oControl, msgFUERA_DE_RANGO); return false; } } else if (control.getAttribute('TipoFecha') == 'true' && control.value != '') { if (!ValidarFecha(control)) { MostrarAlerta(oControl, msgFECHA_INVALIDA); SinErrores = false; } else if (!ValidarRangoFecha(control)) { MostrarAlerta(oControl, msgFUERA_DE_RANGO); SinErrores = false; } } else if (control.getAttribute('TipoHora') == 'true' && control.value != '') { if (!ValidarHora(control)) { MostrarAlerta(oControl, msgHORA_INVALIDA); SinErrores = false; } else if (!ValidarRangoHora(control)) { MostrarAlerta(oControl, msgFUERA_DE_RANGO); SinErrores = false; } } if(SinErrores) { OcultarAlerta(oControl, ""); return true; } else { return false; } } function BlockSubmit() { var SinErrores = BlockValidate(); if(!SinErrores) { alert(msgHAY_ERRORES); return false; } MostrarLoaderPantallaCompleta(); return true; } function BlockValidate() { var SinErrores = true; var i, control; for (i = 0; i < TextBox_Validators.length; i++) { control = TextBox_Validators[i]; if (!Validate(control.id)) { SinErrores = false; } } if (!SinErrores) { return false; } return true; } function MostrarAlerta(oControl, sText) { //Habilita las imágenes de error (signo de admiración rojo...) para el control dado //y setea su "alt" con un mensaje descriptivo del error de validación var control = document.getElementById(oControl); if (control.getAttribute('UsarIconoEnValidacion') != null && control.getAttribute('UsarIconoEnValidacion').toLowerCase() == 'true') { var imgControl = document.getElementById(oControl + "_alerta"); imgControl.style.visibility = 'visible'; imgControl.title = sText; } else { control.style.backgroundColor = '#FFD8CE'; control.title = sText; } } function OcultarAlerta(oControl, sText) { //Oculta las imágenes de error (signo de admiración rojo...) para el control dado var control = document.getElementById(oControl); if (control.getAttribute('UsarIconoEnValidacion') != null && control.getAttribute('UsarIconoEnValidacion').toLowerCase() == 'true') { var imgControl = document.getElementById(oControl + "_alerta"); imgControl.style.visibility = 'hidden'; imgControl.title = ""; } else { control.style.backgroundColor = ''; control.title = ''; } } /**************** FUNCIONES DE VALIDACION *********************/ function ValidarEntero(oControl) { return EsEntero(Trim(oControl.value)); } function ValidarRangoEntero(oControl) { var bRet = false; var Valor = parseInt(oControl.value); //Se asume que value es integer ya validado if(EsEntero(oControl.getAttribute('Minimo')) && EsEntero(oControl.getAttribute('Maximo')) ) { var valMin = parseInt(oControl.getAttribute('Minimo')); var valMax = parseInt(oControl.getAttribute('Maximo')); if(Valor >= valMin && Valor <= valMax) {return true;} else {return false;} } else if(EsEntero(oControl.getAttribute('Minimo')) ) { var valMin = parseInt(oControl.getAttribute('Minimo')); if(Valor >= valMin) {return true;} else {return false;} } else if(EsEntero(oControl.getAttribute('Maximo')) ) { var valMax = parseInt(oControl.getAttribute('Maximo')); if(Valor <= valMax) {return true;} else {return false;} } else {//alert("No se puede determinar máximo y/o mínimo en la definición del campo"); return true;} return bRet; } function parseFloat2(val) { return parseFloat(val.replace('.', '').replace(',', '.')); } function ValidarDecimal(oControl) { if(!oControl.value.length||oControl.disabled) return true; // blank fields are the domain of requireValue var val= oControl.value; //if(typeof(SeparadorDeMiles)!='undefined') val= val.replace(new RegExp(SeparadorDeMiles,'g'),''); val= parseFloat2(oControl.value); if(isNaN(val)) { // error de parseo return false; } //oControl.value= val; return true; } function EsDecimal(str) { if(!str.length) return true; // blank fields are the domain of requireValue var val= str; //if(typeof(SeparadorDeMiles)!='undefined') val= val.replace(new RegExp(SeparadorDeMiles,'g'),''); val = parseFloat2(str); if(isNaN(val)) { // error de parseo return false; } str= val; return true; } function ValidarRangoDecimal(oControl) { var bRet = false; var valMin = oControl.getAttribute('Minimo'); var valMax = oControl.getAttribute('Maximo'); var Valor = parseFloat2(oControl.value); //Se asume que value es integer ya validado if(EsDecimal(oControl.getAttribute('Minimo')) && EsDecimal(oControl.getAttribute('Maximo')) ) { var valMin = parseFloat2(oControl.getAttribute('Minimo')); var valMax = parseFloat2(oControl.getAttribute('Maximo')); if(Valor >= valMin && Valor <= valMax) {return true;} else {return false;} } else if(EsDecimal(oControl.getAttribute('Minimo')) && oControl.getAttribute('Maximo') == "" ) { var valMin = parseFloat2(oControl.getAttribute('Minimo')); if(Valor >= valMin) {return true;} else {return false;} } else if(EsDecimal(oControl.getAttribute('Maximo')) && oControl.getAttribute('Minimo') == "" ) { var valMax = parseFloat2(oControl.getAttribute('Maximo')); if(Valor <= valMax) {return true;} else {return false;} } else {//alert("No se puede determinar máximo y/o mínimo en la definición del campo"); return true;} return bRet; } function ValidarFecha(oControl) { if (oControl.getAttribute('requerido') != 'true' && oControl.value == '') { return true; } return EsFecha(Trim(oControl.value)); } function ValidarFechaHora(oControl) { if (oControl.getAttribute('requerido') != 'true' && oControl.value == '') { return true; } return EsFechaHora(Trim(oControl.value)); } function ValidarRangoFecha(oControl) { var bRet = false; var valMin = FechaDesdeString(Trim(oControl.getAttribute('Minimo'))); var valMax = FechaDesdeString(Trim(oControl.getAttribute('Maximo'))); if(valMin == valMax){return true;} else { var Valor = FechaDesdeString(Trim(oControl.value)); if(Valor >= valMin && Valor <= valMax) {return true;} else {return false;} } return bRet; } function ValidarHora(control) { return EsHora(Trim(control.value)); } function ValidarRangoHora(oControl) { var bRet = false; return true; var valMin = FechaDesdeString(Trim(oControl.getAttribute('Minimo'))); var valMax = FechaDesdeString(Trim(oControl.getAttribute('Maximo'))); if (valMin == valMax) { return true; } else { var Valor = FechaDesdeString(Trim(oControl.value)); if (Valor >= valMin && Valor <= valMax) { return true; } else { return false; } } return bRet; } /* FUNCIONES AUXILIARES */ function VerbalizarRango(oControl) { var strRango = ""; if(oControl.getAttribute('Minimo') != ""){strRango += "mayor o igual a "+ oControl.getAttribute('Minimo');} if(oControl.getAttribute('Minimo') != "" && oControl.getAttribute('Maximo') != ""){strRango += " y ";} if(oControl.getAttribute('Maximo') != ""){strRango += "menor o igual a "+ oControl.getAttribute('Maximo');} return strRango; } function Trim(trimstr) { if(trimstr.length < 1){return "";} trimstr = RTrim(trimstr); trimstr = LTrim(trimstr); if(trimstr==""){return "";}else{return trimstr;} } function RTrim(VALUE) { var w_space = String.fromCharCode(32); var v_length = VALUE.length; var strTemp = ""; if(v_length < 0) { return""; } var iTemp = v_length -1; while(iTemp > -1) { if(VALUE.charAt(iTemp) == w_space) { } else { strTemp = VALUE.substring(0,iTemp +1); break; } iTemp = iTemp-1; } return strTemp; } function LTrim(VALUE) { var w_space = String.fromCharCode(32); if(v_length < 1) { return""; } var v_length = VALUE.length; var strTemp = ""; var iTemp = 0; while(iTemp < v_length) { if(VALUE.charAt(iTemp) == w_space) { } else { strTemp = VALUE.substring(iTemp,v_length); break; } iTemp = iTemp + 1; } //End While return strTemp; } //End Function var minYear=1900; var maxYear=2100; function isEmpty(s) { return ((s == null) || (s.length == 0)) } function isDigit (c) { return ((c >= "0") && (c <= "9")) } function isInteger (s) { var i; if (isEmpty(s)) if (isInteger.arguments.length == 1) return 0; else return (isInteger.arguments[1] == true); for (i = 0; i < s.length; i++) { var c = s.charAt(i); if (!isDigit(c)) return false; } return true; } function EsEntero (s) { if (isEmpty(s)) if (EsEntero.arguments.length == 1) return false; else return (EsEntero.arguments[1] == true); else { var startPos = 0; var secondArg = false; if (EsEntero.arguments.length > 1) secondArg = EsEntero.arguments[1]; // skip leading + or - if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") ) startPos = 1; return (isInteger(s.substring(startPos, s.length), secondArg)) } } function DiasEnFebrero (year){ // Febrero tiene 29 dias en cada año divisible por 4 // exceptuando años "centuriales", que además no son divisibles por 400 return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 ); } function DiasEnMes(n) { for (var i = 1; i <= n; i++) { this[i] = 31 if (i==4 || i==6 || i==9 || i==11) {this[i] = 30} if (i==2) {this[i] = 29} } return this } function EsFechaHora(dateStr) { var partesDateTime = dateStr.split(" "); if (partesDateTime.length != 2) { return false; } return EsFecha(partesDateTime[0]) && EsHora(partesDateTime[1]); } function EsFecha(dateStr) { var datePieces = dateStr.split(SeparadorDeFechas); var month = 0, day = 0, year = 0; if( EsEntero(datePieces[0]) && EsEntero(datePieces[0]) && EsEntero(datePieces[0]) ) { if(FormatoFechas == "mmddaaaa") { month = parseInt(datePieces[0], 10); day = parseInt(datePieces[1], 10); year = parseInt(datePieces[2], 10); } else if(FormatoFechas == "ddmmaaaa") { month = parseInt(datePieces[1], 10); day = parseInt(datePieces[0], 10); year = parseInt(datePieces[2], 10); } } else { return false; } var daysInMonth = DiasEnMes(12); var valida=true; if ( day>31 || (month==2 && day>DiasEnFebrero(year)) || day > daysInMonth[month]){ valida = false; } return valida; } function EsHora(dateStr) { var datePieces = dateStr.split(SeparadorDeHoras); var horas = 0, minutos = 0; if (EsEntero(datePieces[0]) && EsEntero(datePieces[1])) { horas = parseInt(datePieces[0], 10); minutos = parseInt(datePieces[1]); } else { return false; } var valida = true; if (horas > 23 || horas < 0 || minutos > 59 || minutos < 0) { valida = false; } return valida; } function FechaDesdeString(dateStr, oControl) { var datePieces = dateStr.split(SeparadorDeFechas); var month = 0, day = 0, year = 0; if(FormatoFechas == "mmddaaaa") { month = parseInt(datePieces[0], 10); day = parseInt(datePieces[1], 10); year = parseInt(datePieces[2], 10); } else if(FormatoFechas == "ddmmaaaa") { month = parseInt(datePieces[1], 10); day = parseInt(datePieces[0], 10); year = parseInt(datePieces[2], 10); } return new Date(year, month, day).getTime(); }