Validaciones - Alta de: Proveedores, Transportistas, Transportes y Choferes
This commit is contained in:
@@ -44,11 +44,11 @@
|
||||
<div class="content">
|
||||
<h4 class="mb-4">➕ Registro de Proveedor</h4>
|
||||
<div class="card p-4 shadow-sm">
|
||||
<form action="/IMPORTADORES/proveedores/guardar" method="POST">
|
||||
<form action="/IMPORTADORES/proveedores/guardar" method="POST" enctype="multipart/form-data" id="formAltaProveedores">
|
||||
<div class="row g-3">
|
||||
<div class="col-md-4">
|
||||
<label for="Clave" class="form-label">Clave *</label>
|
||||
<input type="text" name="Clave" id="Clave" class="form-control" required>
|
||||
<input type="text" name="Clave" id="Clave" class="form-control" maxlength="20" required>
|
||||
</div>
|
||||
<div class="col-md-8">
|
||||
<label for="Nombre" class="form-label">Nombre *</label>
|
||||
@@ -56,7 +56,7 @@
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<label for="RFC" class="form-label">RFC *</label>
|
||||
<input type="text" name="RFC" id="RFC" class="form-control" required>
|
||||
<input type="text" name="RFC" id="RFC" class="form-control" maxlength="13" required>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<label for="Ciudad" class="form-label">Ciudad *</label>
|
||||
@@ -64,7 +64,7 @@
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<label for="Telefono" class="form-label">Teléfono</label>
|
||||
<input type="tel" name="Telefono" id="Telefono" class="form-control">
|
||||
<input type="tel" name="Telefono" id="Telefono" maxlength="11" class="form-control">
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<label for="Pais" class="form-label">País</label>
|
||||
@@ -84,7 +84,7 @@
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<label for="CodigoPostal" class="form-label">Código Postal</label>
|
||||
<input type="text" name="CodigoPostal" id="CodigoPostal" class="form-control">
|
||||
<input type="text" name="CodigoPostal" id="CodigoPostal" maxlength="5" class="form-control">
|
||||
</div>
|
||||
</div>
|
||||
<div class="mt-4 text-end">
|
||||
@@ -98,5 +98,148 @@
|
||||
<!-- Bootstrap JS Bundle -->
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
|
||||
|
||||
<script>
|
||||
document.getElementById('formAltaProveedores').addEventListener('submit', function(e) {
|
||||
e.preventDefault(); // Prevenir envío por defecto
|
||||
|
||||
// Obtener todos los campos
|
||||
const clave = document.getElementById('Clave').value.trim();
|
||||
const nombre = document.getElementById('Nombre').value.trim();
|
||||
const rfc = document.getElementById('RFC').value.trim().toUpperCase();
|
||||
const ciudad = document.getElementById('Ciudad').value.trim();
|
||||
const telefono = document.getElementById('Telefono').value.trim();
|
||||
const pais = document.getElementById('Pais').value.trim();
|
||||
const entidadFederativa = document.getElementById('EntidadFederativa').value.trim();
|
||||
const municipio = document.getElementById('Municipio').value.trim();
|
||||
const colonia = document.getElementById('Colonia').value.trim();
|
||||
const codigoPostal = document.getElementById('CodigoPostal').value.trim();
|
||||
|
||||
// Expresiones regulares
|
||||
const rfcRegex = /^[A-ZÑ&]{3,4}\d{6}[A-Z0-9]{3}$/;
|
||||
const telefonoRegex = /^\d{3}\s\d{7}$/;
|
||||
const codigoPostalRegex = /^\d{5}$/;
|
||||
const soloLetrasRegex = /^[a-zA-ZáéíóúÁÉÍÓÚñÑ\s]+$/;
|
||||
const alfanumericoRegex = /^[a-zA-Z0-9áéíóúÁÉÍÓÚñÑ\s]+$/;
|
||||
|
||||
// VALIDAR CAMPOS OBLIGATORIOS
|
||||
if (!clave) {
|
||||
Swal.fire({ icon: 'error', title: 'Clave requerida', text: 'Por favor ingresa la clave del proveedor.', confirmButtonColor: '#dc3545' });
|
||||
document.getElementById('Clave').focus();
|
||||
return;
|
||||
}
|
||||
if (clave.length < 2 || clave.length > 20) {
|
||||
Swal.fire({ icon: 'error', title: 'Clave inválida', text: 'La clave debe tener entre 2 y 20 caracteres.', confirmButtonColor: '#dc3545' });
|
||||
document.getElementById('Clave').focus();
|
||||
return;
|
||||
}
|
||||
if (!alfanumericoRegex.test(clave)) {
|
||||
Swal.fire({ icon: 'error', title: 'Clave inválida', text: 'La clave solo puede contener letras, números y espacios.', confirmButtonColor: '#dc3545'
|
||||
});
|
||||
document.getElementById('Clave').focus();
|
||||
return;
|
||||
}
|
||||
if (!nombre) {
|
||||
Swal.fire({ icon: 'error', title: 'Nombre requerido', text: 'Por favor ingresa el nombre del proveedor.', confirmButtonColor: '#dc3545' });
|
||||
document.getElementById('Nombre').focus();
|
||||
return;
|
||||
}
|
||||
if (nombre.length < 2 || nombre.length > 100) {
|
||||
Swal.fire({ icon: 'error', title: 'Nombre inválido', text: 'El nombre debe tener entre 2 y 100 caracteres.', confirmButtonColor: '#dc3545' });
|
||||
document.getElementById('Nombre').focus();
|
||||
return;
|
||||
}
|
||||
if (!rfc) {
|
||||
Swal.fire({ icon: 'error', title: 'RFC requerido', text: 'Por favor ingresa el RFC del proveedor.', confirmButtonColor: '#dc3545' });
|
||||
document.getElementById('RFC').focus();
|
||||
return;
|
||||
}
|
||||
if (!rfcRegex.test(rfc)) {
|
||||
Swal.fire({ icon: 'error', title: 'RFC inválido', text: 'El RFC debe tener 12 o 13 caracteres con el formato correcto (ej. ABC123456XYZ).', confirmButtonColor: '#dc3545' });
|
||||
document.getElementById('RFC').focus();
|
||||
return;
|
||||
}
|
||||
if (!ciudad) {
|
||||
Swal.fire({ icon: 'error', title: 'Ciudad requerida', text: 'Por favor ingresa la ciudad del proveedor.', confirmButtonColor: '#dc3545' });
|
||||
document.getElementById('Ciudad').focus();
|
||||
return;
|
||||
}
|
||||
if (!soloLetrasRegex.test(ciudad)) {
|
||||
Swal.fire({ icon: 'error', title: 'Ciudad inválida', text: 'La ciudad solo puede contener letras y espacios.', confirmButtonColor: '#dc3545' });
|
||||
document.getElementById('Ciudad').focus();
|
||||
return;
|
||||
}
|
||||
// VALIDAR CAMPOS OPCIONALES (solo si tienen contenido)
|
||||
if (telefono) {
|
||||
if (!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 (pais) {
|
||||
if (!soloLetrasRegex.test(pais)) {
|
||||
Swal.fire({ icon: 'error', title: 'País inválido', text: 'El país solo puede contener letras y espacios.', confirmButtonColor: '#dc3545' });
|
||||
document.getElementById('Pais').focus();
|
||||
return;
|
||||
}
|
||||
if (pais.length < 2 || pais.length > 50) {
|
||||
Swal.fire({ icon: 'error', title: 'País inválido', text: 'El país debe tener entre 2 y 50 caracteres.', confirmButtonColor: '#dc3545'
|
||||
});
|
||||
document.getElementById('Pais').focus();
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (entidadFederativa) {
|
||||
if (!soloLetrasRegex.test(entidadFederativa)) {
|
||||
Swal.fire({ icon: 'error', title: 'Entidad Federativa inválida', text: 'La entidad federativa solo puede contener letras y espacios.', confirmButtonColor: '#dc3545' });
|
||||
document.getElementById('EntidadFederativa').focus();
|
||||
return;
|
||||
}
|
||||
if (entidadFederativa.length < 2 || entidadFederativa.length > 50) {
|
||||
Swal.fire({ icon: 'error', title: 'Entidad Federativa inválida', text: 'La entidad federativa debe tener entre 2 y 50 caracteres.', confirmButtonColor: '#dc3545' });
|
||||
document.getElementById('EntidadFederativa').focus();
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (municipio) {
|
||||
if (!soloLetrasRegex.test(municipio)) {
|
||||
Swal.fire({ icon: 'error', title: 'Municipio inválido', text: 'El municipio solo puede contener letras y espacios.', confirmButtonColor: '#dc3545' });
|
||||
document.getElementById('Municipio').focus();
|
||||
return;
|
||||
}
|
||||
if (municipio.length < 2 || municipio.length > 50) {
|
||||
Swal.fire({ icon: 'error', title: 'Municipio inválido', text: 'El municipio debe tener entre 2 y 50 caracteres.', confirmButtonColor: '#dc3545' });
|
||||
document.getElementById('Municipio').focus();
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (colonia) {
|
||||
if (!alfanumericoRegex.test(colonia)) {
|
||||
Swal.fire({ icon: 'error', title: 'Colonia inválida', text: 'La colonia solo puede contener letras, números y espacios.', confirmButtonColor: '#dc3545'
|
||||
});
|
||||
document.getElementById('Colonia').focus();
|
||||
return;
|
||||
}
|
||||
if (colonia.length < 2 || colonia.length > 50) {
|
||||
Swal.fire({ icon: 'error', title: 'Colonia inválida', text: 'La colonia debe tener entre 2 y 50 caracteres.', confirmButtonColor: '#dc3545' });
|
||||
document.getElementById('Colonia').focus();
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (codigoPostal) {
|
||||
if (!codigoPostalRegex.test(codigoPostal)) {
|
||||
Swal.fire({ icon: 'error', title: 'Código Postal inválido', text: 'El código postal debe tener exactamente 5 dígitos.', confirmButtonColor: '#dc3545' });
|
||||
document.getElementById('CodigoPostal').focus();
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Actualizar el valor del RFC en mayúsculas antes de enviar
|
||||
document.getElementById('RFC').value = rfc;
|
||||
|
||||
// SI TODAS LAS VALIDACIONES PASAN, ENVIAR EL FORMULARIO
|
||||
this.submit();
|
||||
});
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user