Search code examples
phpformstwitter-bootstrapvalidationform-control

Using Bootstrap validation to validate form fields


I hope you all can give me a push in the right direction. I am creating a form using html/css & bootstrap. The form works kind of but I would like to use the validation states bootstrap offers. How do I use this in the right way? A field only should go green when it's correctly filled and red when an error occurs.

Can anyone give me an example on how I can show the correct state? (so green when correctly filled, red when not)

<div class="container">
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="row">
<div class="col-xs-3" style="padding-right:5px;">
  <label for="ex1"  ></label>
  <input class="form-control inputForm" id="ex1" name="voornaam" type="text" placeholder="Voornaam">
</div>
<div class="col-xs-3" style="padding-left:5px;">
  <label for="ex2"></label>
  <input class="form-control inputForm" id="ex2" type="text" name="achternaam" placeholder="Achternaam">   </div> </div><div class="row">


<div class="col-xs-6">
  <label for="ex3"></label>
  <input class="form-control inputForm" id="ex3" name="email" type="text" placeholder="Voer je e-mailadres in">

  </div></div>

<div class="row">


<div class="col-xs-6">
  <label for="ex3"></label>
  <input class="form-control inputForm" id="ex3"  name="wachtwoord" type="password" placeholder="Wachtwoord">
</div></div>

<div class="row">


<div class="col-xs-6">

  <input class="btn btn-primary btn-lg active" type="submit"  value="Registeren" style="Float: right; margin-top: 15px; width: 100%;">
</div></div>
</form>
</div>

Solution

  • Use classes .has-warning, .has-error, or .has-success.

    Error state:

    <input class="form-control inputForm has-error" id="ex1" name="voornaam" type="text" placeholder="Voornaam">
    

    Correct state:

    <input class="form-control inputForm has-success" id="ex1" name="voornaam" type="text" placeholder="Voornaam">
    

    http://getbootstrap.com/css/#forms-control-validation