Turns a field into the required form when select Yes

Asked

Viewed 442 times

1

I have a field in my PHP form where I ask if the user has a phone at home. It is a select with YES and NO. If YES the following field must be required to be completed, if NO, the field will not be required.

   <div class="col-sm-4 form-group">
<label for="telcasa"> Telefone de Casa:</label>  
<select required name="telcasa" required  class="form-control" id="telcasa">
    <option value="escolhaopcao">Selecione</option>
    <option value="Sim">Sim</option>
    <option value="Não">Não</option>
  </select>
 </div>   

 <div class="col-sm-8 form-group">
                                    <label for="telefone_casa">Caso sua resposta seja sim, deixe o número aqui: </label>
                                    <input name="telefone_casa" type="number" required class="form-control" id="telefone_casa" maxlength="50">
                                </div> 

How do I make that rule work? If the selection is YES the field If your answer is yes, leave the number here: it will be mandatory, if No, it will not be required.

Follow picture of the fields:

inserir a descrição da imagem aqui

2 answers

0

To the select, the secret is this option <option value="">Selecione</option> How you added the required attribute in select, then the first option must have its value equal to "";
Or Just add the value of the first option equal to "";

As you asked in the question, using JQuery:

Try this code:

$("#telcasa").change(function(){

 let _this =  this;

 if(_this.selectedIndex == 1){

$("#telefone_casa").attr("required",true);

}
else{

$("#telefone_casa").removeAttr("required");

}

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" media="screen" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" />
<label for="telcasa"> Telefone de Casa:</label>  
<select required name="telcasa" required  class="form-control" id="telcasa">
    <option value="">Selecione</option>
    <option value="Sim">Sim</option>
    <option value="Não">Não</option>
  </select>
 </div>   

 <div class="col-sm-8 form-group">
                                    <label for="telefone_casa">Caso sua resposta seja sim, deixe o número aqui: </label>
                                    <input name="telefone_casa" type="number" required class="form-control" id="telefone_casa" maxlength="50">
                                </div> 

Try this example:

https://repl.it/@TaffarelXavier/LatestAustereEfficiency

  • Hi friend, thanks for the contact, did not work :(

  • What’s showing up? What do you really want?

  • So friend, with the selection of the NO the field of the phone_home is still mandatory the filling. It should be released, according to the function you sent me, correct? Do I have to remove the "required" from this line: <input name="phone_home" type="number" required class="form-control" id="phone_home" maxlength="50">

  • In fact, if you are using Jquery, this is already done automatically, as I described in the code I sent.

  • That’s what I figured

  • https://repl.it/@TaffarelXavier/LatestAustereEfficiency

Show 1 more comment

0


You can do with pure JS in a very simple way, if the value of Select for nao, you remove the attribute require of input

By CSS I put a red embroidery on the fields with require, then when you choose NÃO the edge will fade, because it will remove the attribute require of input

inserir a descrição da imagem aqui

let sel = document.getElementById('telcasa');

function verifica() {
    let nao = document.getElementById('telefone_casa');
    if (sel.value == 'nao') {
        nao.required = false;
    } else {
        nao.required = true;
    }
}

sel.addEventListener('change', verifica);
:required {
  border:2px solid red !important;
}
<link rel="stylesheet" type="text/css" media="screen" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" />

<div class="container">
    <div class="row">
        <div class="col-sm-4 form-group">
            <label for="telcasa"> Telefone de Casa:</label>
            <select required name="telcasa" required class="form-control" id="telcasa">
                <option value="escolhaopcao">Selecione</option>
                <option value="sim">Sim</option>
                <option value="nao">Não</option>
            </select>
        </div>

        <div class="col-sm-8 form-group">
            <label for="telefone_casa">Caso sua resposta seja sim, deixe o número aqui: </label>
            <input name="telefone_casa" type="number" required class="form-control" id="telefone_casa" maxlength="50">
        </div>
    </div>
</div>

  • Perfect brother, but here for me it didn’t work! No Dreamweaver dá uma mensagem de erro, There is a syntax error on line 23: Code hinting may not work until you fix this error. This line 23 is this code: Let sel = Document.getElementById('telcasa');

  • @Alessandroramos does not have why not work, you checked if the ID of the element is the same or you changed the name of the ID? There is no reason why not work in VS Code does not accuse any error, and here on the site works perfectly, so it was you who changed something there that made the code stop working. Even if you take my code and test it locally in a separate file your tb will most likely not show errors, copy and paste the answer into a new HTML and test

  • Thanks brother, it worked!!!!

  • @Alessandroramos there yes, had pq not work rss

Browser other questions tagged

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