Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add required attribute to the fileds in react js?

In the code I want to have a required attribute to make the field mandatory . But its not working.. Any simple way in react for this validation?

<label htmlFor="name">Name</label>
<input type="text" className="form-control"
id="name" placeholder="Enter name" required/>
like image 979
ReNinja Avatar asked Oct 26 '25 08:10

ReNinja


1 Answers

Try to add form and button tags:

<form onSubmit={handler}>
  <input type="text" id="name" placeholder="Enter name" required />
  <button type="submit" />
</form

If this doesn't work, try replacing required with required="required"

like image 193
Yossi Avatar answered Oct 28 '25 21:10

Yossi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!