Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining a `required` field in Bootstrap

How do I define text field as required (with red border) in Twitter Bootstrap 3?

required="required" doesn't work...

<form role="form">   <div class="form-group">     <label for="exampleInputEmail1">Email address</label>     <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email">   </div>   <div class="form-group">     <label for="exampleInputPassword1">Password</label>     <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">   </div>   <div class="form-group">     <label for="exampleInputFile">File input</label>     <input type="file" id="exampleInputFile">     <p class="help-block">Example block-level help text here.</p>   </div>   <div class="checkbox">     <label>       <input type="checkbox"> Check me out     </label>   </div>   <button type="submit" class="btn btn-default">Submit</button> </form> 
like image 369
motorcb Avatar asked Oct 28 '13 16:10

motorcb


People also ask

How do you indicate a field is required?

How to indicate a required field. Provide the required text in the label. Provide a graphic * image in the label with appropriate alt text. Providing a star (asterisk) symbol.

How do you create a required field form?

Yes. In your form builder account, click on any field in your Form Editor to open its Edit Field panel on the left side of the page. Tick the checkbox Required field and an asterisk will appear next to the label of the form field. The field is now required and visitors can't submit the form without filling it in.

How do you make a field required in a modal?

If you want to make required fields on a form, all you have to do is add required="required" in the <input> tags.


2 Answers

Try using required="true" in bootstrap 3

like image 60
vma Avatar answered Oct 12 '22 07:10

vma


Update 2018

Since the original answer HTML5 validation is now supported in all modern browsers. Now the easiest way to make a field required is simply using the required attibute.

<input type="email" class="form-control" id="exampleInputEmail1" required>

or in compliant HTML5:

<input type="email" class="form-control" id="exampleInputEmail1" required="true"> 

Read more on Bootstrap 4 validation


In Bootstrap 3, you can apply a "validation state" class to the parent element: http://getbootstrap.com/css/#forms-control-validation

For example has-error will show a red border around the input. However, this will have no impact on the actual validation of the field. You'd need to add some additional client (javascript) or server logic to make the field required.

Demo: http://bootply.com/90564


like image 44
Zim Avatar answered Oct 12 '22 06:10

Zim