Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
Validation continues when input value is deleted

There are validations for mandatory input fields. Validation works when these fields are unfilled or filled incorrectly. After filling the field I delete it but the validation still works and an error message pops up. I don't want validation to run when input value is deleted.

How can I solve 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 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

validator.js

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

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Changing the nameOnChange method like this will solve the problem.

  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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!