D
D
display: block2015-11-04 16:17:21
JavaScript
display: block, 2015-11-04 16:17:21

How to make mandatory one input out of two available?

There is a callback form for contacting a client with 3 fields: name, phone number, email. How to make the validation pass only if one of the contact fields (phone or email) is filled.

Filled in the field "Name" and "Phone" - validation passed.
"Name" and "Email" - validation passed.
Filled only "Name" - validation failed.

codepen.io/anon/pen/epLYbr

Answer the question

In order to leave comments, you need to log in

4 answer(s)
T
TheBububo, 2015-11-04
@qork

All of the above is good, but it was relevant before HTML5. HTML5 has a validation api, and everything else is crutches. jsfiddle.net/ufma1kj4

R
Rikcon, 2015-11-04
@Rikcon

jqueryvalidation.org/require_from_group-method
Download this plugin, and be sure to download additional-methods.js to it on the download page
I sent you the docks, your user case is clearly there.

I
Ivanq, 2015-11-04
@Ivanq

jsfiddle.net/Ivanq/2ac8gc38
See for yourself - when there is no data, the page is not refreshed.

P
petyagrill, 2015-11-04
@petyagrill

This is the direction in which direction to dig

$('form').on('submit', function(e){
 
  var em = $("#email").val();
  var ph = $("#phone").val();
  if ((em == '') && (ph == '')){
    alert('error');
    e.preventDefault();
  } else {
     alert('done')
  }
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question