How to display two TextBox from a result in DropDownList

0

I display a DropDownList with two SC and PR values, if it selects SC then I want it to display two TextBoxes, one with the RG, and another with the Birth Date, and if the person selects PR, I want to get off only a field with the Date of Birth and it can not be a minor.

I'm doing in C # MVC, and I'm having a question about what's going on in Jquery, JavaScript, or another solution.

    
asked by anonymous 04.05.2018 / 02:39

1 answer

0

Assuming you already have jquery embedded in your page.

The fields:

<select name="campoSelecao" id="campoSelecao">
  <option value="SC">SC</option>
  <option value="PR">PR</option>
</select>
<input type="text" name="inputRg" id="inputRg" style="display: none">
<input type="text" name="inputDtNascimento" id="inputDtNascimento" style="display: none">

.js file

$('body').on('change', '#campoSelecao', function () {
            $('#inputRg').css('display','none');
            $('#inputDtNascimento').css('display','none');

            var valorSelecionado = $(this).val();
            if (valorSelecionado == 'SC') {
                $('#inputRg').css('display','block');
                $('#inputDtNascimento').css('display','block');
            }

            if (valorSelecionado == 'PR') {
                $('#inputDtNascimento').css('display','block');
            }
    });

To check the age:

1 - I suggest using a date plugin, for example: link . With this plugin implemented, we will have the date field formatted;

2 - Add the moment.js link plugin. This plugin will make any comparison easier;

3 - Implement the rule in your .js file

$('body').on('focusout', '#inputDtNascimento', function () {
   var nascimento = $(this).val(); //pega a valor do campo data
   var idade = Math.floor(moment(new Date()).diff(moment(nascimento), 'years', true));
   if (idade < 18) {
      $(this).focus(); //da o foco no campo
      $(this).val(''); //limpar o campo
   }
});
    
04.05.2018 / 03:27