php – Validating an input field as a function of a select


I'm working with Codeigniter/Bootstrap and I need to validate a field on my form against another. I have a select field called Contract, with two possible options (Yes or No) and another input field called DataContract. I need that when the option YES is selected in the select, filling out the DataContract field becomes mandatory.


I made a basic example, I don't know if it's the best way, but it works.

<!DOCTYPE html>

    <link href="style.css">
      function ativarInputDataContrato(){
        var lista = document.getElementById("lista-boolean-contrato");
        var input = document.getElementById("data-contrato");
        if(lista.value == "Sim"){
          input.disabled = false;
          input.required = true;
          input.disabled = true;
          input.required = false;

    <form id="formulario">
       <select id="lista-boolean-contrato" onchange="ativarInputDataContrato()">
         <option value="Nao">Não</option>
         <option value="Sim">Sim</option>
       <input id = "data-contrato" type = "date" disabled required="true" />
       <input type = "submit" value = "Enviar"/>

Scroll to Top