-4
I need one of the 2 fields to be filled (mandatory). I am using "bootstrap-Validator": " 0.11.9",
I tried to do with the custom according to the example code, only that I would need to inform for the 2 fields that is ok or false for the 2 fields, I do not know if this is the best way to do for this library.
$(document).ready(function () {
$("#sentMessage").validator({
feedback: {
success: 'glyphicon-ok',
error: 'glyphicon-remove'
},
delay: 500,
custom: {
telefone: function ($el) {
var campo_comparar = $el.data("telefone");
console.log("el val [" + $el.val() + "]");
console.log("campo comparar val [" + $('#' + campo_comparar).val() + "]");
if ($el.val() == "" && $('#' + campo_comparar).val() == "") {
console.log("Preencha o telefone ou o celular");
return true;
} else {
console.log("Tudo ok");
return false;
}
},
},
});
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/1000hz-bootstrap-validator/0.11.9/validator.min.js"></script>
<!-- Page Content -->
<div class="container">
<!-- Contact Form -->
<!-- In order to set the email address and subject line for the contact form go to the bin/contact_me.php file. -->
<div class="row">
<div class="col-md-8">
<form name="sentMessage" id="contactForm" class="form-horizontal" role="form" novalidate data-toggle="validator">
<div class="row">
<div class="col-sm-6">
<div class="control-group form-group">
<label for="telefone" class="col-sm-4 control-label">* Telefone</label>
<div class="col-sm-8">
<input type="tel" class="form-control telefone" id="telefone" required data-required-error="Preencha este campo ou o celular." data-telefone="celular">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="control-group form-group">
<label for="celular" class="col-sm-4 control-label">* Celular</label>
<div class="col-sm-8">
<input type="tel" class="form-control telefone" id="celular" required data-required-error="Preencha este campo ou o telefone." data-telefone="telefone">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
</div>
<div class="control-group form-group">
<div class="col-sm-3 col-sm-offset-5">
<button type="submit" class="btn btn-info btn-block"><b>Enviar</b></button>
</div>
</div>
</form>
</div>
</div>
<!-- /.row -->
</div>
<!-- /.Page Content -->
or
In case you want to create a Validator group, I advise you to do this validation in the form’s Submit function. If not you would have to check if both fields have already been touched which is more complicated.. your feedback with * is already interesting for filling out.
– ruansenadev