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

Custom validation with at least one input #568

Closed proyecto81 closed 7 years ago

proyecto81 commented 7 years ago

Hey! How are you :) I'm triying to make it works, but i can't find a solution. I read all the closed issues, but any example works for me. This is the problem: I've got 3 text input fields, if at least one is complete, the form should be validated.

<input type="email" pattern="^[-\w.]+@{1}[-a-z0-9]+[.]{1}[a-z]{2,5}$" class="form-control" data-personales="email" id="email" name="email">; <input type="text" class="form-control" data-personales="telcel" id="telcel" name="telcel">; <input type="text" class="form-control" data-personales="whatsapp" id="whatsapp" name="whatsapp">

And the validator: $("#frm_personales").validator({ custom: { personales: function($el) { if($('#email').val() === '' || $('#telcel').val() === '' || $('#whatsapp').val() === ''){ return "At least one field must be completed."; }; }}})

It never validates... just always submits the form... otherwise, if i make them all required, the rule is usless. Any comment can be usefull. Tnks! :)

1000hz commented 7 years ago

I think you're probably better off just toggling the required attribute on your inputs as necessary instead of using a custom validator. Here's an example of what I think you want.

http://jsbin.com/cofejowepe/edit?html,js,output