Hướng dẫn bootstrap 3 form validation

Examples and usage guidelines for form control styles, layout options, and custom components for creating a wide variety of forms.

Overview

Bootstrap’s form controls expand on our Rebooted form styles with classes. Use these classes to opt into their customized displays for a more consistent rendering across browsers and devices.

Be sure to use an appropriate type attribute on all inputs (e.g., email for email address or number for numerical information) to take advantage of newer input controls like email verification, number selection, and more.

Here’s a quick example to demonstrate Bootstrap’s form styles. Keep reading for documentation on required classes, form layout, and more.

class="form-group"> for="exampleInputEmail1">Email address type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email"> id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.
class="form-group"> for="exampleInputPassword1">Password type="password" class="form-control" id="exampleInputPassword1" placeholder="Password"> class="form-check"> type="checkbox" class="form-check-input" id="exampleCheck1"> class="form-check-label" for="exampleCheck1">Check me out type="submit" class="btn btn-primary">Submit

Form controls

Textual form controls—like s,

For file inputs, swap the .form-control for .form-control-file.

class="form-group"> for="exampleFormControlFile1">Example file input type="file" class="form-control-file" id="exampleFormControlFile1">

Sizing

Set heights using classes like .form-control-lg and .form-control-sm.

 class="form-control form-control-lg" type="text" placeholder=".form-control-lg">
 class="form-control" type="text" placeholder="Default input">
 class="form-control form-control-sm" type="text" placeholder=".form-control-sm">

 class="form-control form-control-lg">
  Large select

 class="form-control">
  Default select

 class="form-control form-control-sm">
  Small select

Readonly

Add the readonly boolean attribute on an input to prevent modification of the input’s value. Read-only inputs appear lighter (just like disabled inputs), but retain the standard cursor.

 class="form-control" type="text" placeholder="Readonly input here…" readonly>

Readonly plain text

If you want to have elements in your form styled as plain text, use the .form-control-plaintext class to remove the default form field styling and preserve the correct margin and padding.

class="form-group row"> for="staticEmail" class="col-sm-2 col-form-label">Email class="col-sm-10"> type="text" readonly class="form-control-plaintext" id="staticEmail" value=""> class="form-group row"> for="inputPassword" class="col-sm-2 col-form-label">Password class="col-sm-10"> type="password" class="form-control" id="inputPassword" placeholder="Password">

 class="form-inline">
   class="form-group mb-2">
     for="staticEmail2" class="sr-only">Email
     type="text" readonly class="form-control-plaintext" id="staticEmail2" value="">
  
   class="form-group mx-sm-3 mb-2">
     for="inputPassword2" class="sr-only">Password
     type="password" class="form-control" id="inputPassword2" placeholder="Password">
  
   type="submit" class="btn btn-primary mb-2">Confirm identity

Checkboxes and radios

Default checkboxes and radios are improved upon with the help of .form-check, a single class for both input types that improves the layout and behavior of their HTML elements. Checkboxes are for selecting one or several options in a list, while radios are for selecting one option from many.

Disabled checkboxes and radios are supported, but to provide a not-allowed cursor on hover of the parent , you’ll need to add the disabled attribute to the .form-check-input. The disabled attribute will apply a lighter color to help indicate the input’s state.

Checkboxes and radios use are built to support HTML-based form validation and provide concise, accessible labels. As such, our s and s are sibling elements as opposed to an within a . This is slightly more verbose as you must specify id and for attributes to relate the and .

Default (stacked)

By default, any number of checkboxes and radios that are immediate sibling will be vertically stacked and appropriately spaced with .form-check.

Default checkbox

Disabled checkbox

 class="form-check">
   class="form-check-input" type="checkbox" value="" id="defaultCheck1">
   class="form-check-label" for="defaultCheck1">
    Default checkbox
  

 class="form-check">
   class="form-check-input" type="checkbox" value="" id="defaultCheck2" disabled>
   class="form-check-label" for="defaultCheck2">
    Disabled checkbox
  

Default radio

Second default radio

Disabled radio

 class="form-check">
   class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios1" value="option1" checked>
   class="form-check-label" for="exampleRadios1">
    Default radio
  

 class="form-check">
   class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios2" value="option2">
   class="form-check-label" for="exampleRadios2">
    Second default radio
  

 class="form-check disabled">
   class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios3" value="option3" disabled>
   class="form-check-label" for="exampleRadios3">
    Disabled radio
  

Inline

Group checkboxes or radios on the same horizontal row by adding .form-check-inline to any .form-check.

 class="form-check form-check-inline">
   class="form-check-input" type="checkbox" id="inlineCheckbox1" value="option1">
   class="form-check-label" for="inlineCheckbox1">1

 class="form-check form-check-inline">
   class="form-check-input" type="checkbox" id="inlineCheckbox2" value="option2">
   class="form-check-label" for="inlineCheckbox2">2

 class="form-check form-check-inline">
   class="form-check-input" type="checkbox" id="inlineCheckbox3" value="option3" disabled>
   class="form-check-label" for="inlineCheckbox3">3 (disabled)

 class="form-check form-check-inline">
   class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio1" value="option1">
   class="form-check-label" for="inlineRadio1">1

 class="form-check form-check-inline">
   class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio2" value="option2">
   class="form-check-label" for="inlineRadio2">2

 class="form-check form-check-inline">
   class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio3" value="option3" disabled>
   class="form-check-label" for="inlineRadio3">3 (disabled)

Without labels

Add .position-static to inputs within .form-check that don’t have any label text. Remember to still provide some form of label for assistive technologies (for instance, using aria-label).

 class="form-check">
   class="form-check-input position-static" type="checkbox" id="blankCheckbox" value="option1" aria-label="...">

 class="form-check">
   class="form-check-input position-static" type="radio" name="blankRadio" id="blankRadio1" value="option1" aria-label="...">

Layout

Since Bootstrap applies display: block and width: 100% to almost all our form controls, forms will by default stack vertically. Additional classes can be used to vary this layout on a per-form basis.

Form groups

The .form-group class is the easiest way to add some structure to forms. It provides a flexible class that encourages proper grouping of labels, controls, optional help text, and form validation messaging. By default it only applies margin-bottom, but it picks up additional styles in .form-inline as needed. Use it with

s,
s, or nearly any other element.

class="form-group"> for="formGroupExampleInput">Example label type="text" class="form-control" id="formGroupExampleInput" placeholder="Example input">
class="form-group"> for="formGroupExampleInput2">Another label type="text" class="form-control" id="formGroupExampleInput2" placeholder="Another input">

Form grid

More complex forms can be built using our grid classes. Use these for form layouts that require multiple columns, varied widths, and additional alignment options.

class="row"> class="col"> type="text" class="form-control" placeholder="First name"> class="col"> type="text" class="form-control" placeholder="Last name">

Form row

You may also swap .row for .form-row, a variation of our standard grid row that overrides the default column gutters for tighter and more compact layouts.

class="form-row"> class="col"> type="text" class="form-control" placeholder="First name"> class="col"> type="text" class="form-control" placeholder="Last name">

More complex layouts can also be created with the grid system.

class="form-row"> class="form-group col-md-6"> for="inputEmail4">Email type="email" class="form-control" id="inputEmail4" placeholder="Email"> class="form-group col-md-6"> for="inputPassword4">Password type="password" class="form-control" id="inputPassword4" placeholder="Password"> class="form-group"> for="inputAddress">Address type="text" class="form-control" id="inputAddress" placeholder="1234 Main St"> class="form-group"> for="inputAddress2">Address 2 type="text" class="form-control" id="inputAddress2" placeholder="Apartment, studio, or floor"> class="form-row"> class="form-group col-md-6"> for="inputCity">City type="text" class="form-control" id="inputCity"> class="form-group col-md-4"> for="inputState">State id="inputState" class="form-control"> selected>Choose... ... class="form-group col-md-2"> for="inputZip">Zip type="text" class="form-control" id="inputZip"> class="form-group"> class="form-check"> class="form-check-input" type="checkbox" id="gridCheck"> class="form-check-label" for="gridCheck"> Check me out type="submit" class="btn btn-primary">Sign in

Horizontal form

Create horizontal forms with the grid by adding the .row class to form groups and using the .col-*-* classes to specify the width of your labels and controls. Be sure to add .col-form-label to your s as well so they’re vertically centered with their associated form controls.

At times, you maybe need to use margin or padding utilities to create that perfect alignment you need. For example, we’ve removed the padding-top on our stacked radio inputs label to better align the text baseline.

class="form-group row"> for="inputEmail3" class="col-sm-2 col-form-label">Email class="col-sm-10"> type="email" class="form-control" id="inputEmail3" placeholder="Email"> class="form-group row"> for="inputPassword3" class="col-sm-2 col-form-label">Password class="col-sm-10"> type="password" class="form-control" id="inputPassword3" placeholder="Password"> class="form-group"> class="row"> class="col-form-label col-sm-2 pt-0">Radios class="col-sm-10"> class="form-check"> class="form-check-input" type="radio" name="gridRadios" id="gridRadios1" value="option1" checked> class="form-check-label" for="gridRadios1"> First radio class="form-check"> class="form-check-input" type="radio" name="gridRadios" id="gridRadios2" value="option2"> class="form-check-label" for="gridRadios2"> Second radio class="form-check disabled"> class="form-check-input" type="radio" name="gridRadios" id="gridRadios3" value="option3" disabled> class="form-check-label" for="gridRadios3"> Third disabled radio
class="form-group row"> class="col-sm-2">Checkbox class="col-sm-10"> class="form-check"> class="form-check-input" type="checkbox" id="gridCheck1"> class="form-check-label" for="gridCheck1"> Example checkbox class="form-group row"> class="col-sm-10"> type="submit" class="btn btn-primary">Sign in

Horizontal form label sizing

Be sure to use .col-form-label-sm or .col-form-label-lg to your s or s to correctly follow the size of .form-control-lg and .form-control-sm.

class="form-group row"> for="colFormLabelSm" class="col-sm-2 col-form-label col-form-label-sm">Email class="col-sm-10"> type="email" class="form-control form-control-sm" id="colFormLabelSm" placeholder="col-form-label-sm"> class="form-group row"> for="colFormLabel" class="col-sm-2 col-form-label">Email class="col-sm-10"> type="email" class="form-control" id="colFormLabel" placeholder="col-form-label"> class="form-group row"> for="colFormLabelLg" class="col-sm-2 col-form-label col-form-label-lg">Email class="col-sm-10"> type="email" class="form-control form-control-lg" id="colFormLabelLg" placeholder="col-form-label-lg">

Column sizing

As shown in the previous examples, our grid system allows you to place any number of .cols within a .row or .form-row. They’ll split the available width equally between them. You may also pick a subset of your columns to take up more or less space, while the remaining .cols equally split the rest, with specific column classes like .col-7.

class="form-row"> class="col-7"> type="text" class="form-control" placeholder="City"> class="col"> type="text" class="form-control" placeholder="State"> class="col"> type="text" class="form-control" placeholder="Zip">

Auto-sizing

The example below uses a flexbox utility to vertically center the contents and changes .col to .col-auto so that your columns only take up as much space as needed. Put another way, the column sizes itself based on the contents.

class="form-row align-items-center"> class="col-auto"> class="sr-only" for="inlineFormInput">Name type="text" class="form-control mb-2" id="inlineFormInput" placeholder="Jane Doe"> class="col-auto"> class="sr-only" for="inlineFormInputGroup">Username class="input-group mb-2"> class="input-group-prepend"> class="input-group-text">@ type="text" class="form-control" id="inlineFormInputGroup" placeholder="Username"> class="col-auto"> class="form-check mb-2"> class="form-check-input" type="checkbox" id="autoSizingCheck"> class="form-check-label" for="autoSizingCheck"> Remember me class="col-auto"> type="submit" class="btn btn-primary mb-2">Submit

You can then remix that once again with size-specific column classes.

class="form-row align-items-center"> class="col-sm-3 my-1"> class="sr-only" for="inlineFormInputName">Name type="text" class="form-control" id="inlineFormInputName" placeholder="Jane Doe"> class="col-sm-3 my-1"> class="sr-only" for="inlineFormInputGroupUsername">Username class="input-group"> class="input-group-prepend"> class="input-group-text">@ type="text" class="form-control" id="inlineFormInputGroupUsername" placeholder="Username"> class="col-auto my-1"> class="form-check"> class="form-check-input" type="checkbox" id="autoSizingCheck2"> class="form-check-label" for="autoSizingCheck2"> Remember me class="col-auto my-1"> type="submit" class="btn btn-primary">Submit

And of course custom form controls are supported.

class="form-row align-items-center"> class="col-auto my-1"> class="mr-sm-2" for="inlineFormCustomSelect">Preference class="custom-select mr-sm-2" id="inlineFormCustomSelect"> selected>Choose... value="1">One value="2">Two value="3">Three class="col-auto my-1"> class="custom-control custom-checkbox mr-sm-2"> type="checkbox" class="custom-control-input" id="customControlAutosizing"> class="custom-control-label" for="customControlAutosizing">Remember my preference class="col-auto my-1"> type="submit" class="btn btn-primary">Submit

Inline forms

Use the .form-inline class to display a series of labels, form controls, and buttons on a single horizontal row. Form controls within inline forms vary slightly from their default states.

  • Controls are display: flex, collapsing any HTML white space and allowing you to provide alignment control with spacing and flexbox utilities.
  • Controls and input groups receive width: auto to override the Bootstrap default width: 100%.
  • Controls only appear inline in viewports that are at least 576px wide to account for narrow viewports on mobile devices.

You may need to manually address the width and alignment of individual form controls with spacing utilities (as shown below). Lastly, be sure to always include a with each form control, even if you need to hide it from non-screenreader visitors with .sr-only.

 class="form-inline">
   class="sr-only" for="inlineFormInputName2">Name
   type="text" class="form-control mb-2 mr-sm-2" id="inlineFormInputName2" placeholder="Jane Doe">

   class="sr-only" for="inlineFormInputGroupUsername2">Username
   class="input-group mb-2 mr-sm-2">
     class="input-group-prepend">
       class="input-group-text">@
    
     type="text" class="form-control" id="inlineFormInputGroupUsername2" placeholder="Username">
  

   class="form-check mb-2 mr-sm-2">
     class="form-check-input" type="checkbox" id="inlineFormCheck">
     class="form-check-label" for="inlineFormCheck">
      Remember me
    
  

   type="submit" class="btn btn-primary mb-2">Submit

Custom form controls and selects are also supported.

 class="form-inline">
   class="my-1 mr-2" for="inlineFormCustomSelectPref">Preference
   class="custom-select my-1 mr-sm-2" id="inlineFormCustomSelectPref">
     selected>Choose...
     value="1">One
     value="2">Two
     value="3">Three
  

   class="custom-control custom-checkbox my-1 mr-sm-2">
     type="checkbox" class="custom-control-input" id="customControlInline">
     class="custom-control-label" for="customControlInline">Remember my preference
  

   type="submit" class="btn btn-primary my-1">Submit

Alternatives to hidden labels

Assistive technologies such as screen readers will have trouble with your forms if you don’t include a label for every input. For these inline forms, you can hide the labels using the .sr-only class. There are further alternative methods of providing a label for assistive technologies, such as the aria-label, aria-labelledby or title attribute. If none of these are present, assistive technologies may resort to using the placeholder attribute, if present, but note that use of placeholder as a replacement for other labelling methods is not advised.

Help text

Block-level help text in forms can be created using .form-text (previously known as .help-block in v3). Inline help text can be flexibly implemented using any inline HTML element and utility classes like .text-muted.

Associating help text with form controls

Help text should be explicitly associated with the form control it relates to using the aria-describedby attribute. This will ensure that assistive technologies—such as screen readers—will announce this help text when the user focuses or enters the control.

Help text below inputs can be styled with .form-text. This class includes display: block and adds some top margin for easy spacing from the inputs above.

Password Your password must be 8-20 characters long, contain letters and numbers, and must not contain spaces, special characters, or emoji.

 for="inputPassword5">Password
 type="password" id="inputPassword5" class="form-control" aria-describedby="passwordHelpBlock">
 id="passwordHelpBlock" class="form-text text-muted">
  Your password must be 8-20 characters long, contain letters and numbers, and must not contain spaces, special characters, or emoji.

Inline text can use any typical inline HTML element (be it a , , or something else) with nothing more than a utility class.

 class="form-inline">
   class="form-group">
     for="inputPassword6">Password
     type="password" id="inputPassword6" class="form-control mx-sm-3" aria-describedby="passwordHelpInline">
     id="passwordHelpInline" class="text-muted">
      Must be 8-20 characters long.
    
  

Disabled forms

Add the disabled boolean attribute on an input to prevent user interactions and make it appear lighter.

 class="form-control" id="disabledInput" type="text" placeholder="Disabled input here..." disabled>

Add the disabled attribute to a

to disable all the controls within.

disabled> class="form-group"> for="disabledTextInput">Disabled input type="text" id="disabledTextInput" class="form-control" placeholder="Disabled input"> class="form-group"> for="disabledSelect">Disabled select menu id="disabledSelect" class="form-control"> Disabled select class="form-check"> class="form-check-input" type="checkbox" id="disabledFieldsetCheck" disabled> class="form-check-label" for="disabledFieldsetCheck"> Can't check this type="submit" class="btn btn-primary">Submit

Caveat with anchors

By default, browsers will treat all native form controls (, ,