Validaciones - Alta de: Proveedores, Transportistas, Transportes y Choferes
This commit is contained in:
@@ -35,7 +35,7 @@
|
||||
<div class="content">
|
||||
<h4>✏️ Editar Chofer #<?= (int)$chofer['id_chofer'] ?></h4>
|
||||
<div class="card p-4 shadow-sm bg-white">
|
||||
<form action="/IMPORTADORES/choferes/actualizar" method="POST" enctype="multipart/form-data">
|
||||
<form action="/IMPORTADORES/choferes/actualizar" method="POST" enctype="multipart/form-data" id="formEditChoferes">
|
||||
<input type="hidden" name="id_chofer" value="<?= (int)$chofer['id_chofer'] ?>">
|
||||
|
||||
<!-- Transportista -->
|
||||
@@ -72,14 +72,14 @@
|
||||
<!-- Licencia -->
|
||||
<div class="col-md-6 mb-3">
|
||||
<label for="numero_licencia" class="form-label">Número de Licencia</label>
|
||||
<input name="numero_licencia" id="numero_licencia" type="text" class="form-control"
|
||||
<input name="numero_licencia" id="numero_licencia" type="text" maxlength="11" class="form-control"
|
||||
value="<?= htmlspecialchars($chofer['numero_licencia']) ?>" required>
|
||||
</div>
|
||||
|
||||
<!-- Teléfono -->
|
||||
<div class="col-md-6 mb-3">
|
||||
<label for="telefono" class="form-label">Teléfono</label>
|
||||
<input name="telefono" id="telefono" type="tel" class="form-control"
|
||||
<input name="telefono" id="telefono" type="tel" maxlength="11" class="form-control"
|
||||
value="<?= htmlspecialchars($chofer['telefono']) ?>">
|
||||
</div>
|
||||
</div>
|
||||
@@ -128,6 +128,94 @@
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
document.getElementById('formEditChoferes').addEventListener('submit', function(e) {
|
||||
e.preventDefault(); // Prevenir envío por defecto
|
||||
|
||||
// Obtener todos los campos correctamente
|
||||
const transportista_id = document.getElementById('transportista_id').value;
|
||||
const nombre = document.getElementById('nombre').value.trim();
|
||||
const apellido = document.getElementById('apellido').value.trim();
|
||||
const numero_licencia = document.getElementById('numero_licencia').value.trim();
|
||||
const telefono = document.getElementById('telefono').value.trim();
|
||||
const email = document.getElementById('email').value.trim();
|
||||
const fecha_ingreso = document.getElementById('fecha_ingreso').value;
|
||||
const foto = document.getElementById('foto').files[0];
|
||||
|
||||
// Expresiones regulares
|
||||
const telefonoRegex = /^\d{3}\s\d{7}$/;
|
||||
const emailRegex = /^[a-zA-Z0-9._+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
|
||||
const soloNumerosRegex = /^[0-9]+$/;
|
||||
|
||||
// VALIDAR CAMPOS OBLIGATORIOS PRIMERO
|
||||
if (!transportista_id) {
|
||||
Swal.fire({ icon: 'error', title: 'Transportista requerido', text: 'Por favor selecciona un transportista.', confirmButtonColor: '#dc3545' });
|
||||
document.getElementById('transportista_id').focus();
|
||||
return;
|
||||
}
|
||||
if (!nombre) {
|
||||
Swal.fire({ icon: 'error', title: 'Nombre requerido', text: 'Por favor ingresa el nombre.', confirmButtonColor: '#dc3545' });
|
||||
document.getElementById('nombre').focus();
|
||||
return;
|
||||
}
|
||||
if (!apellido) {
|
||||
Swal.fire({ icon: 'error', title: 'Apellido requerido', text: 'Por favor ingresa el apellido.', confirmButtonColor: '#dc3545' });
|
||||
document.getElementById('apellido').focus();
|
||||
return;
|
||||
}
|
||||
if (!numero_licencia) {
|
||||
Swal.fire({ icon: 'error', title: 'Número de Licencia requerido', text: 'Por favor ingresa el número de licencia.', confirmButtonColor: '#dc3545' });
|
||||
document.getElementById('numero_licencia').focus();
|
||||
return;
|
||||
}
|
||||
if (!soloNumerosRegex.test(numero_licencia)) {
|
||||
Swal.fire({ icon: 'error', title: 'Número de Licencia inválido', text: 'El número de licencia solo puede contener números', confirmButtonColor: '#dc3545' });
|
||||
document.getElementById('Clave').focus();
|
||||
return;
|
||||
}
|
||||
// VALIDAR CAMPOS OPCIONALES (solo si tienen contenido)
|
||||
if (telefono && !telefonoRegex.test(telefono)) {
|
||||
Swal.fire({ icon: 'error', title: 'Teléfono inválido', text: 'Debe tener 10 dígitos en el formato: 3 dígitos (LADA), espacio, y 7 dígitos. Ejemplo: 123 4567890', confirmButtonColor: '#dc3545' });
|
||||
document.getElementById('telefono').focus();
|
||||
return;
|
||||
}
|
||||
if (email && !emailRegex.test(email)) {
|
||||
Swal.fire({ icon: 'error', title: 'Email inválido', text: 'Por favor ingresa una dirección de email válida.', confirmButtonColor: '#dc3545' });
|
||||
document.getElementById('email').focus();
|
||||
return;
|
||||
}
|
||||
// VALIDAR FECHA DE INGRESO (solo si se proporciona)
|
||||
if (fecha_ingreso) {
|
||||
const fechaIngreso = new Date(fecha_ingreso);
|
||||
const fechaActual = new Date();
|
||||
if (fechaIngreso > fechaActual) {
|
||||
Swal.fire({ icon: 'error', title: 'Fecha inválida', text: 'La fecha de ingreso no puede ser futura.', confirmButtonColor: '#dc3545' });
|
||||
document.getElementById('fecha_ingreso').focus();
|
||||
return;
|
||||
}
|
||||
}
|
||||
// VALIDAR FOTO (solo si se seleccionó)
|
||||
if (foto) {
|
||||
// Validar tamaño (máximo 2 MB)
|
||||
if (foto.size > 2 * 1024 * 1024) {
|
||||
Swal.fire({ icon: 'error', title: 'Foto demasiado grande', text: 'La imagen no debe exceder 2 MB.', confirmButtonColor: '#dc3545' });
|
||||
document.getElementById('foto').focus();
|
||||
return;
|
||||
}
|
||||
// Validar tipo de archivo
|
||||
const tiposPermitidos = ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'];
|
||||
if (!tiposPermitidos.includes(foto.type)) {
|
||||
Swal.fire({ icon: 'error', title: 'Tipo de archivo inválido', text: 'Solo se permiten archivos de imagen (JPG, PNG, GIF).', confirmButtonColor: '#dc3545' });
|
||||
document.getElementById('foto').focus();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// SI TODAS LAS VALIDACIONES PASAN, ENVIAR EL FORMULARIO
|
||||
this.submit();
|
||||
});
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user