Hide mandatory and non-compulsory field

Asked

Viewed 419 times

0

I have a form. In this form I have two radiobutton with two options.

1) Questao 1 (Obrigatorio)
1 = Sim 
2 = Nao 

______________________

2) Questao 2(Obrigatorio)
1 = Vivo
2 = Morto

If this question 1 is = Yes Question 2 loses its obligation, it will be a mandatory question to be answered, but if it is = No, question 2 will return with the obligation.

Does anyone have any way to help me with this?

Thank you in advance.

3 answers

5


Folks good afternoon I made the following validation

var value = $('input[name="CAMPO1"]').val();

if (value == '1') {
    $('[name="CAMPO2"]').rules('remove', 'required');
} else {
    $('[name="CAMPO2"]').rules('add', 'required');
}

and worked perfectly. I appreciate the help!

1

If you are using client validation in javascript, you can add the attribute data-val-required and remove it when you don’t want the field to be mandatory, something like that:

// Obrigatório
$(campo).attr("data-val-required", "*");

// Não obrigatório
$(campo).removeAttr("data-val-required");

You may need to inform the form to parse the controls again, which you can do using this command:

$.validator
        .unobtrusive
        .parse("form");

0

Then I’d need to create a Validator, implementing IValidatable and IClientValidatable. A suggestion would be to create something like [Required], but only if another property or condition were true.

  • So, I have a file called validacao.js in it I do the validations of the questions and masks etc as an example. $("#form-panel-Altahospitalar"). validate({ errorClass: "input-error", Rules: { dtahosp: "required", },

  • You are using unobtrusive-validation? Or simply jquery-validate?

  • only the jquery-validate

Browser other questions tagged

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