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

133
Views
Controlled form: display a div if invalid input

For a training, I'am trying to check if the input value of this code base includes a "@" when onBlur:

<form>
  <input
    type='text'
    name='my_input'
    value={inputValue}
    onChange={(e) => setInputValue(e.target.value)}
    onBlur={(e) => checkEmail(e.target.value)}
    ></input>
  <button>Save</button>
</form>

Therefore I added a function to display a <div> below the form (or at least below the input field) if there is no "@" in the input, but without success: ๐Ÿง

function checkEmail(value) {
  if (!value.includes('@')) {
    <div>โš ๏ธ Warning, there is no @, this is not a valid email address.</div>;
  }
}

Knowing console.log(!value.includes('@')) well return true. ๐Ÿ˜ซ

Any clue on how to get the <div>โš ๏ธ Warning, there is no @, this is not a valid email address.</div> shown below the form or input when invalid input?

Thanks in advance. ๐Ÿ™‚

about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

You are returning your JSX in the checkEmail function, which isn't quite right. What you want to do instead is have a flag that indicates whether the warning JSX should be rendered, and setup a logic to do so if it is true. inputValue being a state is really handy in this case, one small && operator should handle the rendering function.

<form> ... </form>

{ inputValue && !inputValue.includes('@') && 
(<div>โš ๏ธ Warning, there is no @, this is not a valid email address.</div>) }

PS: you can get rid of onBlur={(e) => checkEmail(e.target.value)} and its handler method.

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!