0

I'm using BootstrapValidator plugin to validate a form, however i have following problem. I have a "Phone" field and a "Mobile" field if the user does not enter either of them, I wanted to launch a custom message (you need to inform one phone number), and if he inform any (phone or mobile) validation would be satisfied.

The doubt is: Is it possible to use conditional inside BootstrapValidator?

johnkavanagh
  • 4,614
  • 2
  • 25
  • 37
Paulo Reis
  • 15
  • 1
  • 10

1 Answers1

1

This seems to be working for a lot of people derived from this post:

$('form').validate({
    rules: {
        Phone: {
            required: true
        },
        Mobile: {
            required: true
        }
    },
    highlight: function(element) {
        $(element).closest('.form-group').addClass('has-error');
    },
    unhighlight: function(element) {
        $(element).closest('.form-group').removeClass('has-error');
    },
    errorElement: 'span',
    errorClass: 'help-block',
    errorPlacement: function(error, element) {
        if(element.parent('.input-group').length) {
            error.insertAfter(element.parent());
        } else {
            error.insertAfter(element);
        }
    }
});

and the html:

<form>
    <div class="form-group">
        <label class="control-label" for="Phone">Phone:</label>
        <div class="input-group">
            <input class="form-control" name="Phone" type="text" />
        </div>
    </div>

    <div class="form-group">
        <label class="control-label" for="Mobile">Mobile:</label>
        <div class="input-group">
            <input class="form-control" name="Mobile" type="text" />
        </div>
    </div>

        <button type="submit" class="btn btn-primary">Submit</button>
</form>
Community
  • 1
  • 1
Barney
  • 2,355
  • 3
  • 22
  • 37