Bootstrap Validation

Provide valuable, actionable feedback to your users with HTML5 form validation – available in all our supported browsers. Choose from the browser default validation feedback, or implement custom messages with our built-in classes and starter JavaScript.

Warning

We currently recommend using custom validation styles, as native browser default validation messages are not consistently exposed to assistive technologies in all browsers (most notably, Chrome on desktop and mobile).


How it works

Here’s how form validation works with Bootstrap:

  • HTML form validation is applied via CSS’s two pseudo-classes, :invalid and :valid. It applies to <input>, <select>, and <textarea> elements.
  • Bootstrap scopes the :invalid and :valid styles to parent .was-validated class, usually applied to the <form>. Otherwise, any required field without a value shows up as invalid on page load. This way, you may choose when to activate them (typically after form submission is attempted).
  • To reset the appearance of the form (for instance, in the case of dynamic form submissions using AJAX), remove the .was-validated class from the <form> again after submission.
  • As a fallback, .is-invalid and .is-valid classes may be used instead of the pseudo-classes for server side validation. They do not require a .was-validated parent class.
  • Due to constraints in how CSS works, we cannot (at present) apply styles to a <label> that comes before a form control in the DOM without the help of custom JavaScript.
  • All modern browsers support the constraint validation API, a series of JavaScript methods for validating form controls.
  • Feedback messages may utilize the browser defaults (different for each browser, and unstylable via CSS) or our custom feedback styles with additional HTML and CSS.
  • You may provide custom validity messages with setCustomValidity in JavaScript.

With that in mind, consider the following demos for our custom form validation styles, optional server side classes, and browser defaults.


Custom styles

For custom Bootstrap form validation messages, you’ll need to add the noValidate attribute to your <form>. This disables the browser default feedback tooltips, but still provides access to the form validation APIs in JavaScript. Try to submit the form below; our JavaScript will intercept the submit button and relay feedback to you.

When attempting to submit, you’ll see the :invalid and :valid styles applied to your form controls.

Looks good!
Looks good!
We'll never share your email with anyone else.
Please provide a valid city.
Please provide a valid state.
Please provide a valid zip.
You must agree before submitting.
            
                import React from 'react';
import { Container, Row, Col } from 'mdbreact';

class FormsPage extends React.Component  {

  state = {
    fname: 'Mark',
    lname: 'Otto',
    email: 'examplde@gmail.com',
    city: '',
    state: '',
    zip: ''
  }

  submitHandler = (event) => {
    event.preventDefault();
    event.target.className += ' was-validated';
  }

  changeHandler = (event) => {
    this.setState({...this.state, [event.target.name]: event.target.value})
  }

  render() {
    return(
      <Container className="mt-5">
        <Row className="mt-6">
          <Col md="">
            <form className='needs-validation' onSubmit={this.submitHandler} noValidate>
            <Row>
              <div className="col-md-4 mb-3">
                <label htmlFor="defaultFormRegisterNameEx" className="grey-text">First name</label>
                <input value={this.state.fname} name='fname' onChange={this.changeHandler} type="text" id="defaultFormRegisterNameEx" className="form-control" placeholder="First name" required/>
                <div className="valid-feedback">Looks good!</div>
              </div>
              <div className="col-md-4 mb-3">
                <label htmlFor="defaultFormRegisterEmailEx2" className="grey-text">Last name</label>
                <input value={this.state.lname} name='lname' onChange={this.changeHandler} type="text" id="defaultFormRegisterEmailEx2" className="form-control" placeholder="Last name" required/>
                <div className="valid-feedback">Looks good!</div>
              </div>
              <div className="col-md-4 mb-3">
                <label htmlFor="defaultFormRegisterConfirmEx3" className="grey-text">Email</label>
                <input value={this.state.email} onChange={this.changeHandler} type="email" id="defaultFormRegisterConfirmEx3" className="form-control" name='email' placeholder="Your Email address"/>
                <small id="emailHelp" className="form-text text-muted">We'll never share your email with anyone else.</small>
              </div>
            </Row>
            <Row>
              <div className="col-md-4 mb-3">
                <label htmlFor="defaultFormRegisterPasswordEx4" className="grey-text">City</label>
                <input value={this.state.city} onChange={this.changeHandler} type="text" id="defaultFormRegisterPasswordEx4" className="form-control" name='city' placeholder="City" required/>
                <div className="invalid-feedback">Please provide a valid city.</div>
                <div className="valid-feedback">Looks good!</div>
              </div>
              <div className="col-md-4 mb-3">
                <label htmlFor="defaultFormRegisterPasswordEx4" className="grey-text">State</label>
                <input value={this.state.state} onChange={this.changeHandler} type="text" id="defaultFormRegisterPasswordEx4" className="form-control" name='state' placeholder="State" required/>
                <div className="invalid-feedback">Please provide a valid state.</div>
                <div className="valid-feedback">Looks good!</div>
              </div>
              <div className="col-md-4 mb-3">
                <label htmlFor="defaultFormRegisterPasswordEx4" className="grey-text">Zip</label>
                <input value={this.state.zip} onChange={this.changeHandler} type="text" id="defaultFormRegisterPasswordEx4" className="form-control" name='zip' placeholder="Zip" required/>
                <div className="invalid-feedback">Please provide a valid zip.</div>
                <div className="valid-feedback">Looks good!</div>
              </div>
            </Row>
            <div className="col-md-4 mb-3">
              <div className="form-check pl-0">
                <input className="form-check-input" type="checkbox" value="" id="invalidCheck" required />
                <label className="form-check-label" htmlFor="invalidCheck">
                  Agree to terms and conditions
                </label>
                <div className="invalid-feedback">
                  You must agree before submitting.
                </div>
              </div>
            </div>
            <button className="btn btn-unique" type="submit">Submit Form</button>
            </form>
          </Col>
        </Row>
      </Container>
    );
  }
};

export default FormsPage;
            
          

Browser defaults

Not interested in custom validation feedback messages or writing JavaScript to change form behaviors? All good, you can use the browser defaults. Try submitting the form below. Depending on your browser and OS, you’ll see a slightly different style of feedback.

While these feedback styles cannot be styled with CSS, you can still customize the feedback text through JavaScript.

We'll never share your email with anyone else.
          import React from 'react';
              import { Container, Row, Col } from 'mdbreact';

              class FormsPage extends React.Component  {

                state = {
                  fname: 'Mark',
                  lname: 'Otto',
                  email: 'example@gmail.com',
                  city: '',
                  state: '',
                  zip: ''
                }

                submitHandler = (event) => {
                  event.preventDefault();
                }

                changeHandler = (event) => {
                  this.setState({...this.state, [event.target.name]: event.target.value})
                }

                render() {
                  return(
                    <Container className="mt-5">
                      <Row className="mt-6">
                        <Col md="">
                          <form onSubmit={this.submitHandler}>
                          <Row>
                            <div className="col-md-4 mb-3">
                              <label htmlFor="defaultFormRegisterNameEx" className="grey-text">First name</label>
                              <input value={this.state.fname} name='fname' onChange={this.changeHandler} type="text" id="defaultFormRegisterNameEx" className="form-control" placeholder="First name" required/>
                            </div>
                            <div className="col-md-4 mb-3">
                              <label htmlFor="defaultFormRegisterEmailEx2" className="grey-text">Last name</label>
                              <input value={this.state.lname} name='lname' onChange={this.changeHandler} type="text" id="defaultFormRegisterEmailEx2" className="form-control" placeholder="Last name" required/>
                            </div>
                            <div className="col-md-4 mb-3">
                              <label htmlFor="defaultFormRegisterConfirmEx3" className="grey-text">Email</label>
                              <input value={this.state.email} onChange={this.changeHandler} type="email" id="defaultFormRegisterConfirmEx3" className="form-control" name='email' placeholder="Your Email address"/>
                              <small id="emailHelp" className="form-text text-muted">We'll never share your email with anyone else.</small>
                            </div>
                          </Row>
                          <Row>
                            <div className="col-md-4 mb-3">
                              <label htmlFor="defaultFormRegisterPasswordEx4" className="grey-text">City</label>
                              <input value={this.state.city} onChange={this.changeHandler} type="text" id="defaultFormRegisterPasswordEx4" className="form-control" name='city' placeholder="City" required/>
                            </div>
                            <div className="col-md-4 mb-3">
                              <label htmlFor="defaultFormRegisterPasswordEx4" className="grey-text">State</label>
                              <input value={this.state.state} onChange={this.changeHandler} type="text" id="defaultFormRegisterPasswordEx4" className="form-control" name='state' placeholder="State" required/>
                            </div>
                            <div className="col-md-4 mb-3">
                              <label htmlFor="defaultFormRegisterPasswordEx4" className="grey-text">Zip</label>
                              <input value={this.state.zip} onChange={this.changeHandler} type="text" id="defaultFormRegisterPasswordEx4" className="form-control" name='zip' placeholder="Zip" required/>
                            </div>
                          </Row>
                          <div className="col-md-4 mb-3">
                            <div className="form-check pl-0">
                              <input className="form-check-input" type="checkbox" value="" id="invalidCheck" required />
                              <label className="form-check-label" htmlFor="invalidCheck">
                                Agree to terms and conditions
                              </label>
                            </div>
                          </div>
                          <button className="btn btn-unique" type="submit">Submit Form</button>
                          </form>
                        </Col>
                      </Row>
                    </Container>
                  );
                }
              };

              export default FormsPage;
              
        

Server side

We recommend using client side validation, but in case you require server side, you can indicate invalid and valid form fields with .is-invalid and .is-valid. Note that .invalid-feedback is also supported with these classes.

Looks good!
Looks good!
We'll never share your email with anyone else.
Please provide a valid city.
Please provide a valid state.
Please provide a valid zip.
You must agree before submitting.
          import React from 'react';
              import { Container, Row, Col } from 'mdbreact';

              class FormsPage extends React.Component  {

                state = {
                  fname: 'Mark',
                  lname: 'Otto',
                  email: 'example@gmail.com',
                  city: '',
                  state: '',
                  zip: ''
                }

                submitHandler = (event) => {
                  event.preventDefault();
                  event.target.className += ' was-validated';
                }

                changeHandler = (event) => {
                  this.setState({...this.state, [event.target.name]: event.target.value})
                }

                render() {
                  return(
                    <Container className="mt-5">
                      <Row className="mt-6">
                        <Col md="">
                          <form className='needs-validation' onSubmit={this.submitHandler} noValidate>
                          <Row>
                            <div className="col-md-4 mb-3">
                              <label htmlFor="defaultFormRegisterNameEx" className="grey-text">First name</label>
                              <input value={this.state.fname} name='fname' onChange={this.changeHandler} type="text" id="defaultFormRegisterNameEx" className="form-control is-valid" placeholder="First name" required/>
                              <div className="valid-feedback">Looks good!</div>
                            </div>
                            <div className="col-md-4 mb-3">
                              <label htmlFor="defaultFormRegisterEmailEx2" className="grey-text">Last name</label>
                              <input value={this.state.lname} name='lname' onChange={this.changeHandler} type="text" id="defaultFormRegisterEmailEx2" className="form-control is-valid" placeholder="Last name" required/>
                              <div className="valid-feedback">Looks good!</div>
                            </div>
                            <div className="col-md-4 mb-3">
                              <label htmlFor="defaultFormRegisterConfirmEx3" className="grey-text">Email</label>
                              <input value={this.state.email} onChange={this.changeHandler} type="email" id="defaultFormRegisterConfirmEx3" className="form-control is-valid" name='email' placeholder="Your Email address"/>
                              <small id="emailHelp" className="form-text text-muted">We'll never share your email with anyone else.</small>
                            </div>
                          </Row>
                          <Row>
                            <div className="col-md-4 mb-3">
                              <label htmlFor="defaultFormRegisterPasswordEx4" className="grey-text">City</label>
                              <input value={this.state.city} onChange={this.changeHandler} type="text" id="defaultFormRegisterPasswordEx4" className="form-control is-invalid" name='city' placeholder="City" required/>
                              <div className="invalid-feedback">Please provide a valid city.</div>
                              <div className="valid-feedback">Looks good!</div>
                            </div>
                            <div className="col-md-4 mb-3">
                              <label htmlFor="defaultFormRegisterPasswordEx4" className="grey-text">State</label>
                              <input value={this.state.state} onChange={this.changeHandler} type="text" id="defaultFormRegisterPasswordEx4" className="form-control is-invalid" name='state' placeholder="State" required/>
                              <div className="invalid-feedback">Please provide a valid state.</div>
                              <div className="valid-feedback">Looks good!</div>
                            </div>
                            <div className="col-md-4 mb-3">
                              <label htmlFor="defaultFormRegisterPasswordEx4" className="grey-text">Zip</label>
                              <input value={this.state.zip} onChange={this.changeHandler} type="text" id="defaultFormRegisterPasswordEx4" className="form-control is-invalid" name='zip' placeholder="Zip" required/>
                              <div className="invalid-feedback">Please provide a valid zip.</div>
                              <div className="valid-feedback">Looks good!</div>
                            </div>
                          </Row>
                          <div className="col-md-4 mb-3">
                            <div className="form-check pl-0">
                              <input className="form-check-input is-invalid" type="checkbox" value="" id="invalidCheck" required />
                              <label className="form-check-label" htmlFor="invalidCheck">
                                Agree to terms and conditions
                              </label>
                              <div className="invalid-feedback">
                                You must agree before submitting.
                              </div>
                            </div>
                          </div>
                          <button className="btn btn-unique" type="submit">Submit Form</button>
                          </form>
                        </Col>
                      </Row>
                    </Container>
                  );
                }
              };

              export default FormsPage;
              
        

Supported elements

Our example forms show native textual <input>s above, but form validation styles are available for our custom form controls, too.

Example invalid feedback text
More example invalid feedback text
Example invalid custom select feedback
Example invalid custom file feedback
          import React from 'react';
              import { Container, Row, Col } from 'mdbreact';

              class FormsPage extends React.Component  {
                render() {
                  return(
                    <Container className="mt-5">
                      <Row className="mt-6">
                        <Col md="">
                          <form className='was-validated' noValidate>
                            <div className="custom-control custom-checkbox mb-3">
                              <input type="checkbox" className="custom-control-input" id="customControlValidation1" required />
                              <label className="custom-control-label" htmlFor="customControlValidation1">Check this custom checkbox</label>
                              <div className="invalid-feedback">Example invalid feedback text</div>
                            </div>

                            <div className="custom-control custom-radio">
                              <input type="radio" className="custom-control-input" id="customControlValidation2" name="radio-stacked" required />
                              <label className="custom-control-label" htmlFor="customControlValidation2">Toggle this custom radio</label>
                            </div>
                            <div className="custom-control custom-radio mb-3">
                              <input type="radio" className="custom-control-input" id="customControlValidation3" name="radio-stacked" required />
                              <label className="custom-control-label" htmlFor="customControlValidation3">Or toggle this other custom radio</label>
                              <div className="invalid-feedback">More example invalid feedback text</div>
                            </div>

                            <div className="form-group">
                              <select className="custom-select browser-default" required>
                                <option value="">Open this select menu</option>
                                <option value="1">One</option>
                                <option value="2">Two</option>
                                <option value="3">Three</option>
                              </select>
                              <div className="invalid-feedback">Example invalid custom select feedback</div>
                            </div>

                            <div className="custom-file">
                              <input type="file" className="custom-file-input" id="validatedCustomFile" required />
                              <label className="custom-file-label" htmlFor="validatedCustomFile">Choose file...</label>
                              <div className="invalid-feedback">Example invalid custom file feedback</div>
                            </div>
                          </form>
                        </Col>
                      </Row>
                    </Container>
                  );
                }
              };

              export default FormsPage;
              
        

Tooltips

If your form layout allows it, you can swap the .{valid|invalid}-feedback classes for .{valid|invalid}-tooltip classes to display validation feedback in a styled tooltip. Be sure to have a parent with position: relative on it for tooltip positioning. In the example below, our column classes have this already, but your project may require an alternative setup.

Looks good!
Looks good!
We'll never share your email with anyone else.
Please provide a valid city.
Please provide a valid state.
Please provide a valid zip.
            
                import React from 'react';
import { Container, Row, Col } from 'mdbreact';

class FormsPage extends React.Component  {

  state = {
    fname: 'Mark',
    lname: 'Otto',
    email: 'example@gmail.com',
    city: '',
    state: '',
    zip: ''
  }

  submitHandler = (event) => {
    event.preventDefault();
    event.target.className += ' was-validated';
  }

  changeHandler = (event) => {
    this.setState({...this.state, [event.target.name]: event.target.value})
  }

  render() {
    return(
      <Container className="mt-5">
        <Row className="mt-6">
          <Col md="">
            <form className='needs-validation' onSubmit={this.submitHandler} noValidate>
            <Row>
              <div className="col-md-4 mb-3">
                <label htmlFor="defaultFormRegisterNameEx" className="grey-text">First name</label>
                <input value={this.state.fname} name='fname' onChange={this.changeHandler} type="text" id="defaultFormRegisterNameEx" className="form-control" placeholder="First name" required/>
                <div style={{top: 'auto'}} className="valid-tooltip">Looks good!</div>
              </div>
              <div className="col-md-4 mb-3">
                <label htmlFor="defaultFormRegisterEmailEx2" className="grey-text">Last name</label>
                <input value={this.state.lname} name='lname' onChange={this.changeHandler} type="text" id="defaultFormRegisterEmailEx2" className="form-control" placeholder="Last name" required/>
                <div style={{top: 'auto'}} className="valid-tooltip">Looks good!</div>
              </div>
              <div className="col-md-4 mb-3">
                <label htmlFor="defaultFormRegisterConfirmEx3" className="grey-text">Email</label>
                <input value={this.state.email} onChange={this.changeHandler} type="email" id="defaultFormRegisterConfirmEx3" className="form-control" name='email' placeholder="Your Email address"/>
                <small id="emailHelp" className="form-text text-muted">We'll never share your email with anyone else.</small>
              </div>
            </Row>
            <Row>
              <div className="col-md-4 mb-3">
                <label htmlFor="defaultFormRegisterPasswordEx4" className="grey-text">City</label>
                <input value={this.state.city} onChange={this.changeHandler} type="text" id="defaultFormRegisterPasswordEx4" className="form-control" name='city' placeholder="City" required/>
                <div style={{top: 'auto'}} className="invalid-tooltip">Please provide a valid city.</div>
                <div style={{top: 'auto'}} className="valid-tooltip">Looks good!</div>
              </div>
              <div className="col-md-4 mb-3">
                <label htmlFor="defaultFormRegisterPasswordEx4" className="grey-text">State</label>
                <input value={this.state.state} onChange={this.changeHandler} type="text" id="defaultFormRegisterPasswordEx4" className="form-control" name='state' placeholder="State" required/>
                <div style={{top: 'auto'}} className="invalid-tooltip">Please provide a valid state.</div>
                <div style={{top: 'auto'}} className="valid-tooltip">Looks good!</div>
              </div>
              <div className="col-md-4 mb-3">
                <label htmlFor="defaultFormRegisterPasswordEx4" className="grey-text">Zip</label>
                <input value={this.state.zip} onChange={this.changeHandler} type="text" id="defaultFormRegisterPasswordEx4" className="form-control" name='zip' placeholder="Zip" required/>
                <div style={{top: 'auto'}} className="invalid-tooltip">Please provide a valid zip.</div>
                <div style={{top: 'auto'}} className="valid-tooltip">Looks good!</div>
              </div>
            </Row>
            <button className="btn btn-unique" type="submit">Submit Form</button>
            </form>
          </Col>
        </Row>
      </Container>
    );
  }
};

export default FormsPage;