Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

135
Vistas
La validación continúa cuando se elimina el valor de entrada

Hay validaciones para campos de entrada obligatorios. La validación funciona cuando estos campos no están completos o se completan incorrectamente. Después de llenar el campo, lo elimino, pero la validación aún funciona y aparece un mensaje de error. No quiero que se ejecute la validación cuando se elimina el valor de entrada.

¿Cómo puedo resolverlo?

js

 const [nameValidationState, setNameValidationState] = useState({ error: false, helperText: '', }); const nameOnChange = (event) => { if (nameValidator(event.target.value)) { setNameValidationState({ error: false, helperText: '' }); setPaymentInfo({ ...paymentInfo, firstName: event.target.value, }); } else { setNameValidationState({ error: true, helperText: 'Please enter your name.' }); setPaymentInfo({ ...paymentInfo, firstName: null, }); } }; const handleInputChange = (event) => { const { name, value } = event.target; setPaymentInfo({ ...paymentInfo, [name]: value }); }; const handleFirstNameChange = (event) => { nameOnChange(event); handleInputChange(event); };

html

 <AS.TextField id="outlined-basic" label={t('general.name')} variant="outlined" sx={{ width: '100%', maxWidth: '500px', margin: 'auto' }} InputLabelProps={{ shrink: true, }} placeholder="" onChange={handleFirstNameChange} error={nameValidationState.error} helperText={nameValidationState.helperText} name="firstName" value={paymentInfo.firstName} /> <AS.TextField

validador.js

 export const nameValidator = (name) => { const nameRegex = /^[a-zA-ZwığüşöçĞÜŞÖÇİ]+$/; return nameRegex.test(name); };
about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

Cambiar el método nameOnChange de esta manera resolverá el problema.

 const nameOnChange = (event) => { if (nameValidator(event.target.value)) { setNameValidationState({ error: false, helperText: '' }); setPaymentInfo({ ...paymentInfo, firstName: event.target.value, }); } else { setNameValidationState({ error: true, helperText: 'Please enter your name.' }); setPaymentInfo({ ...paymentInfo, firstName: null, }); } if (event.target.value === '') { setNameValidationState({ error: false, helperText: '' }); } };
about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda