Make one of the two be required

0

Good, I would like to know how I could do for javascript if an email field is not filled in, please put it on the phone, and vice versa

Tel&#233;fono:<br> <input id="inputTelf"  name="telf" type="number"  min="111111111" max="999999999" autocomplete="off">

Email:<br> <input id="inputEmail" name="email" size="20" type="email"  autocomplete="off">

Thank you.

    
asked by Seyker 21.02.2017 в 10:55
source

2 answers

0

Another option to perform the validation:

validate = function(event) {
	event.preventDefault();
  var tel = event.target.querySelector('#phone');
  var email = event.target.querySelector('#email');
  var validation = (tel.value.trim() && email.value.trim());
	
  // Optional
  notifyAlert(validation);

  return validation;
}

notifyAlert = function(validation) {
	var status = (validation) ? 'success' : 'error';
  alert('Validation ${status}');
}
body {font-family: Arial;}
.form {margin: 1rem;}
.form__group {margin: 0 0 1rem 0;}
.form__group label {
  display: block;
  margin: 0 0 .5rem 0;
}
<form onsubmit="validate(event)" class="form">
<div class="form__group">
  <label for="phone">Tel&#233;fono:</label>
  <input id="phone"  name="telf" type="number"  min="111111111" max="999999999" autocomplete="off">
</div>
<div class="form__group">
  <label for="email">Email</label>
  <input id="email" name="email" size="20" type="email" autocomplete="off">
</div>
<button type="submit">Enviar</button>
</form>
    
answered by 21.02.2017 / 14:21
source
1

This could be done by validating the fields when submitting the form. You should add the onsubmit="return validar()" property to the form tag first and the validar() function could be something like this:

<script type="text/javascript">
function validar() {
  var ok = true;
  var msg = "Debe informar el telefono o el email!";
  if(document.form.inputTelf.value == "" && document.form.inputEmail.value == "")
  {
    ok = false;
  }

  if(ok == false)
    alert(msg);
  return ok;
}
</script>
    
answered by 21.02.2017 в 11:11