Make field mandatory depending on response

Asked

Viewed 2,989 times

1

I have a medical form, and the following amendment has been requested:

If field 1 is checked = 1, make field 2 mandatory. If not checked do not make it mandatory.

Could someone help me?

3 answers

4

You can do it like this:

const camp1 = document.getElementById('campo1');
const camp2 = document.getElementById('campo2');
const btn_sub = document.getElementById('btn-submit');

btn_sub.addEventListener('click', function() {
  if(camp1.checked && !camp2.checked) {
    alert('Campo 2 obrigatório');
    return;
  }
  alert('É Válido')
});
<input id="campo1" type="checkbox">1
<br>
<input id="campo2" type="checkbox">2
<button id="btn-submit">Enviar</button>

  • So the question is this, the fields of this form are all required, and I wanted that example: if I select 1 in the answer from above, the low question that is required, let save without filling, I want to remove the requirement. <b>RULE</b> diaposah: the rule for the 'Will you need dialysis after hospital discharge? 'was 'mandatory if stvaUTI and/or stvahosp = 1'. It should be changed to 'mandatory if stvahosp = 1'

  • I put the forms to download at the link below [ [4shared.com/office/G-Ai_xqcba/formulary.html? ]

  • No, I haven’t solved that problem yet!

  • Click on this online Notepad https://shrib.com/yrlt9iuZQNZGJ1w

  • https://jsfiddle.net/7xdjqbbL/

  • The Input "Vital status at hospital discharge" 2 = Dead - makes the question "Will you need dialysis after hospital discharge?" Mandatory

  • @Leonardomacedo, see if this works, I added an id to the input that says "yes", https://jsfiddle.net/7xdjqbbL/6/

  • didn’t work :(

  • yes, error on btn_sub.addeventlistener('click', Function() {

  • What is the @Leonardomacedo error? You have to put your Ubmit button id in javascript on this line const btn_sub = document.getElementById('ID_DO_BOTAO');

  • I clicked inspected my browser page and gave the following error ----- "Uncaught Typeerror: Cannot read Property 'addeventlistener' of null at Altauti? cadastre ID=164:706 "

  • Do what I told you at the top, you have to put your button id in javascript

  • console error came out, only it didn’t work yet

  • Please avoid long discussions in the comments; your talk was moved to the chat

Show 9 more comments

1

var camp1 = $('campo1');
var camp2 = $('campo1');
if(camp1.val() == 1){
  camp2.attr('required', true);
}

You can use getElementById

var camp1 = getElementById('#camp1');
var camp2 = getElementById('#camp2');
if(camp1.val() == 1){
  camp2.attr('required', true);
}
  • So the question is this, the fields of this form are all required, and I wanted that example: if I select 1 in the answer from above, the low question that is required, let save without filling, I want to remove the requirement. <b>RULE</b> diaposah: the rule for the 'Will you need dialysis after hospital discharge? 'was 'mandatory if stvaUTI and/or stvahosp = 1'. It should be changed to 'mandatory if stvahosp = 1'

  • @Leonardomacedo Could you pass the form so I can analyze?

  • good morning, http://www.4shared.com/office/G-AI_xQcba/formulario.html? I put the form for download.

  • @Leonardomacedo was a long time without touching the PC so I couldn’t be here to take a look. You already managed to solve the problem ?

1

Follow example using JavaScript, Jquery and the function required HTML5.

$('.pergunta').change(function() {
  var chars = parseInt(this.value);
  if (chars === 1) {
    $('#retorno').html('<input type="checkbox" name="campo2" required=""> Obrigatório');
    return;
  } else {
    $('#retorno').html('<input type="checkbox" name="campo2"> Não Obrigatório');
    return;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" class="pergunta" name="checado" value="1" required>Sim
<input type="checkbox" class="pergunta" name="checado" value="2" required>Não

<div id="retorno"></div>

I hope to help.

  • So the question is this, the fields of this form are all required, and I wanted that example: if I select 1 in the answer from above, the low question that is required, let save without filling, I want to remove the requirement. <b>RULE</b> diaposah: the rule for the 'Will you need dialysis after hospital discharge? 'was 'mandatory if stvaUTI and/or stvahosp = 1'. It should be changed to 'mandatory if stvahosp = 1'

Browser other questions tagged

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