1000hz / bootstrap-validator

A user-friendly HTML5 form validation jQuery plugin for Bootstrap 3
http://1000hz.github.io/bootstrap-validator
MIT License
2.38k stars 1.07k forks source link

Select Box #618

Open CEscorcio opened 6 years ago

CEscorcio commented 6 years ago

How can the validation work in selection option? I have the follow code, and i want the get an validation error if i dont choose any shop.

  <select name="place" class="form-control" required="required" data-error="Pick ur favorite shop">
          <option value="">Pick ur place</option>
          <option value="BR">Braga</option>
          <option value="FR">Faro</option>
          <option value="LR">Leiria</option>
          <option value="LX">Lisboa
          <option value="PT">Porto</option>
      </select>
      <div class="help-block with-errors"></div>
M3ssman commented 6 years ago

Hello!

I managed a similar case by using custom validations:

let opts = {
  custom: {
    my_select_valiadator: function ($el) {
      if($el.val() == "" ) {
        return "please select something!";     
     }
  }
};
$("select").validate(opts);

and then add to your inspected select element the attribute "data-my_select_validator="my_select_validator"

Greetings