Field obligation

Asked

Viewed 229 times

0

I have two text type fields, where one is zip code and the other is Address.

/in the ZIP code field I use the ZIP code API, when I type in a ZIP code it completes the other fields with the results. I need a following validation:

  • Perform a validation in 2 fields
  • IF ZIP FIELD IS EMPTY,
  • ADDRESS MUST BE MANDATORY IF NOT,
  • ZIP CODE IS MANDATORY

Follow a small image of my form

inserir a descrição da imagem aqui

Could someone help me?

  • How do you fill in the information, or at the end through a button? Or both?

  • @Everson has a button that does the validation at the end of the form

  • @Renan already tried and I could not do the validation

  • Required always requires to be filled in, so it cannot be used for the same validation.

  • @Everson yes, only if one is white the other will be required vice versa

  • 1

    For the sake of logic: https://jsfiddle.net/pf1b7moa/

  • @Everson that’s right, it worked!

  • @Leonardomacedo my answer served ? because you modified it.

Show 3 more comments

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>
<input type="text" value="" placeholder="CEP" name="cep" id="cep">
<input type="text" value="" placeholder="Endereço" name="endereco" id="endereco">
<input type="button" value="Salvar">

Browser other questions tagged

You are not signed in. Login or sign up in order to post.