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

206
Vistas
Validation works when you leave a space in the input field

There are entries that need to be filled in, and those entries have validations. For example, if I talk about name validation; A user can have a first and middle name (I'm not talking about the user's last name). Validation does not work when the username is entered. No problem so far. However, when you leave a space and enter the middle name, the validation works. I don't want validation to run when I enter middle name after space. How can I do it?

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 handleFirstNameChange = (event) => {
  nameOnChange(event);
  handleInputChange(event);
};

validator

export const nameValidator = (name) => {
  const nameRegex = /^[a-zA-ZwığüşöçĞÜŞÖÇİ]+$/;

  return nameRegex.test(name);
};

html

<AS.TextField
  placeholder=""
  onChange={handleFirstNameChange}
  error={nameValidationState.error}
  helperText={nameValidationState.helperText}
  name="firstName"
  value={paymentInfo.firstName}
/>
about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

Would this help you?

let text = " Middle Name"; // Starts with empty space

if(text.startsWith(" ")) { // Checks if it starts with empty space
   // TRUE
   // YOUR LOGIC
} else {
   // FALSE
   // YOUR LOGIC
}

EDIT: Here is a list of string methods you can do in javascript, it will interest you: https://www.w3schools.com/jsref/jsref_obj_string.asp

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