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

172
Views
Why validation is not working for the below case in functional componenet

I need to add validation for name field in reactjs functional component but don't know why its not working ? I have just added valditaion form firstname and clicking on the submit button i need to show the validation if firstname is undefined. Whats wrong in my code ?

const [state, setState] = React.useState({
    data: {
      firstName: 'milan',
      lastName: 'sai',
      number: '999,
    },
    firstNameValid: '',
    lastNameValid: '',
    numberValid: '',
});

const handleTextareaChange = (event) => {
    const { data } = state;
    data[event.target.name] = event.target.value;
    setState({
      data,
    });
};

const valid = () => {
    if (state.data.firstName === 'undefined') {
      setState({ firstNameValid: 'Required.Please enter your given name.' });
    }
};

return (
    <div>
      <VerticalSpacing size={ABLE_SPACING_SIZE.spacing4x} />
      <h1 tabIndex="-1" className="HeadingB mt-sheet-heading">
        {CMS.heading1}
      </h1>
      <VerticalSpacing size={ABLE_SPACING_SIZE.spacing3x} />
      <TextField id="givenName" name="firstName" label={CMS.name} onChange={handleTextareaChange} value={state.data.firstName} />
      <p>{state.firstNameValid}</p>
      <VerticalSpacing size={ABLE_SPACING_SIZE.spacing3x} />
      <TextField id="familyName" name="lastName" label={CMS.familyName} onChange={handleTextareaChange} value={state.data.lastName} />
      <p>{state.lastNameValid}</p>
      <VerticalSpacing size={ABLE_SPACING_SIZE.spacing3x} />
      <TextField id="mobileNumber" name="number" label={CMS.mobile} onChange={handleTextareaChange} value={state.data.number} />
      <p>{state.numberValid}</p>
      <VerticalSpacing size={ABLE_SPACING_SIZE.spacing4x} />
      <ActionButton className={styles.saveCta} variant="HighEmphasis" label={CMS.saveCTA} onClick={() => valid()} />
    </div>
  );
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should not check if given value is undefined using quotes like 'undefined'. Change this line

const valid = () => {
    if (state.data.firstName === 'undefined') {
      setState({ firstNameValid: 'Required.Please enter your given name.' });
    }
};

to

const valid = () => {
    if (!state.data.firstName) {
      setState({
        ...state,
        firstNameValid: "Required.Please enter your given name."
      });
    } else {
      setState({
        ...state,
        firstNameValid: ""
      });
    }
};

You can take a look at this sandbox for live working example.

about 4 years ago · Juan Pablo Isaza 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!