ValidadorFecha.js
Código PHP:
/********************************************************/
function clsValidator()
{
/* Data members */
this.msgError=""; // Return the msg error
this.errorColor="#FFC1C1"; // color de error x defecto
this.head = "ERROR: ... "; // encabezado del mensaje de error x defecto
this.formatoFecha = "ISO" // formato de fecha x defecto
this.error = false;
/* Methods */
///////////////////////////////////////////////////////////
function setEncabezado(head)
{
this.head = head;
}
///////////////////////////////////////////////////////////
function AgregarError() {
this.error=true;
this.msgError += "* "+arguments[0]+"\n";
for (var i=1; i < arguments.length ; i++)
{
document.getElementById(arguments[i]).style.backgroundColor=this.errorColor;
}
}
///////////////////////////////////////////////////////////
function EliminarError()
{
for (var i=0; i < arguments.length ; i++)
{
document.getElementById(arguments[i]).style.backgroundColor="";
}
}
///////////////////////////////////////////////////////////
function setErrorColor(color)
{
this.errorColor = color;
}
///////////////////////////////////////////////////////////
function setFormatoFecha(value)
{
this.formatoFecha = value;
}
///////////////////////////////////////////////////////////
function Vacio(field,msg)
{
this.EliminarError(field);
if (document.getElementById(field).value.replace(/ /g, '') == "")
{
this.AgregarError(msg, field);
return false
}
return true
}
///////////////////////////////////////////////////////////
function Fecha(field, msg)
{
this.EliminarError(field);
var datePat;
var formatoCorrecto;
switch(this.formatoFecha) {
case 'ISO':
datePat = /^(?:(?:(?:(?:1[6-9]|[2-9]d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(/|-|.)(?:0?21(?:29))$)|(?:(?:1[6-9]|[2-9]d)?d{2})(/|-|.)(?:(?:(?:0?[13578]|1[02])2(?:31))|(?:(?:0?[1,3-9]|1[0-2])2(29|30))|(?:(?:0?[1-9])|(?:1[0-2]))2(?:0?[1-9]|1d|2[0-8]))$/;
formatoCorrecto = "aaaa/mm/dd";
break;
case 'EURO':
datePat = /^((([0][1-9]|[12][d])|[3][01])[-/]([0][13578]|[1][02])[-/][1-9]ddd)|((([0][1-9]|[12][d])|[3][0])[-/]([0][13456789]|[1][012])[-/][1-9]ddd)|(([0][1-9]|[12][d])[-/][0][2][-/][1-9]d([02468][048]|[13579][26]))|(([0][1-9]|[12][0-8])[-/][0][2][-/][1-9]ddd)$/;
formatoCorrecto = "dd/mm/aaaa";
break;
case 'ANSI':
datePat = /^((d{2}(([02468][048])|([13579][26]))[-/s]?((((0?[13578])|(1[02]))[-/s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[-/s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[-/s]?((0?[1-9])|([1-2][0-9])))))|(d{2}(([02468][1235679])|([13579][01345789]))[-/s]?((((0?[13578])|(1[02]))[-/s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[-/s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[-/s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(s(((0?[1-9])|(1[0-2])):([0-5][0-9])((s)|(:([0-5][0-9])s))([AM|PM|am|pm]{2,2})))?$/;
formatoCorrecto = "aaaa/mm/dd hh:mm:ss am/pm";
break;
}
var matchArray = document.getElementById(field).value.match(datePat);
if (matchArray != null) return true
else
{
msg += ' [formato: '+formatoCorrecto+']'
this.AgregarError(msg, field);
return false;
}
}
///////////////////////////////////////////////////////////
function Longitud(field, operator, length, msg)
{
this.EliminarError(field);
if (operator == "=") operator = "==";
if ( !(eval("document.getElementById(field).value.length "+operator+" length")) )
{
this.AgregarError(msg, field);
return false
}
return true
}
///////////////////////////////////////////////////////////
function Valor(field, operator, valor, msg)
{
this.EliminarError(field);
if (operator == "=") operator = "==";
if ( !(eval("document.getElementById(field).value "+operator+" valor")) )
{
this.AgregarError(msg, field);
return false
}
return true
}
///////////////////////////////////////////////////////////
function Validar()
{
return !this.error;
}
///////////////////////////////////////////////////////////
function getErrors()
{
alert(this.head+"\n\n"+this.msgError);
}
///////////////////////////////////////////////////////////
this.setEncabezado = setEncabezado;
this.setErrorColor = setErrorColor;
this.setFormatoFecha = setFormatoFecha;
this.getErrors = getErrors;
this.AgregarError = AgregarError;
this.EliminarError = EliminarError;
this.Vacio = Vacio;
this.Fecha = Fecha;
this.Validar = Validar;
}
//////////////////////////////////////////////////////////////////
index.php
Código PHP:
<html>
<head>
<script language="JavaScript" type="text/JavaScript">
<!--
function validate()
{
var Validar = new clsValidator();
//**********************************************************
//Uso todos los valores por defecto
//**********************************************************
//Se podrian cambiar:
//Validar.setEncabezado("[- Hay errores en el formulario -]");
//Validar.setErrorColor("#FFCC00");
//validar.setFormatoHora('24');
//validar.setFormatoFecha('ISO');
//5- Valido que la fecha sea correcta
Validar.Fecha("fechafact", "Fecha de facturacion no válida");
Validar.Fecha("fechadesp", "Fecha de despacho no válida");
//Disparo la Validación
if (Validar.Validar())
alert("Formato de fecha es correcto ... presione enter para continuar");
else Validar.getErrors();
}//Fin function validate()
//-->
</script>
</head>
<body>
<script language="JavaScript" type="text/javascript" srcValidadorFecha.js">
<form name = forma action = "" method = post id="forma" onSubmit="return jsValidateForm(this)">
<input type="hidden" name="mode" value="submit">
<?php
<input name="fechafact" type="text" id="fechafact" value="<?=$fechafact?>" size="10" maxlength="10" style="background:#ffffff" onFocus="encender(this)" onBlur="apagar(this)" onKeyPress="return tabular(event,this)">
</font></font></font></font></font></font></font> </td>
<input name="fechadesp" type="text" id="fechadesp" value="<?=$fechadesp?>" size="10" maxlength="10" style="background:#ffffff" onFocus="encender(this)" onBlur="apagar(this)" onKeyPress="return tabular(event,this)">
<input name="siguiente" id="siguiente" type="submit" class="botones" onClick="validate()" value="Siguiente">
</body>
</html>