Required fields

0

I have two text fields, where one is a ZIP code and the other is Address.

In the CEP field I use the CEP's API, when I type a CEP it completes the other fields with the results. I need the following validation:

  • Validate in 2 fields
  • IF THE ZIP CODE is EMPTY,
  • ADDRESS should be mandatory, IF NOT,
  • ZIP is MANDATORY

Follow a small image of my form

Could anyone help me?

    
asked by anonymous 26.06.2017 / 19:34

1 answer

0

Functional example, anything post in the comments.

$(function(){
  $('input').on('focusout',function(){
    if(this.value === ''){
      $(this).css('border','1px solid red');
    }else{
      $(this).css('border','');
    }
  });
  
  $('input[type=button]').click(function(){
    $('input').each(function(){
      if(this.value === ''){
        alert('Campo vazio.');
        $(this).css('border','1px solid red');
        return false;
      }
    });
  });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="text" value="" placeholder="CEP" name="cep" id="cep">
<input type="text" value="" placeholder="Endereço" name="endereco" id="endereco">
<input type="button" value="Salvar">
    
26.06.2017 / 20:29