0

I want to disable the textbox if the checkbox is unchecked. View:

@Html.CheckBoxFor(model => model.prop1, new { onchange = "OnChange(this,'prop2')" })
...
@Html.TextBoxFor(model => model.prop2)

JavaScript handler

function OnChange(cb, id) 
    {
        if ($(cb).is(':checked') == false)
        {
            $('input#' + id).val('');
            $('input#' + id).attr('disabled', 'disabled');
        }else {
            $('input#' + id).attr('disabled', '');
            $('input#' + id).change();
        }
}

But there is a problem with validation messages. If prop2 is for example marked as "[Required]" when the textbox is disabled error message should disappear. And when the user check the checkbox I should enable the textbox field and turn on the validation

What should I do for this?

1 Answers1

0

Please check following questions:

Asp.net MVC 3 Conditional validation with DataAnnotations

MVC custom validation: compare two dates

Community
  • 1
  • 1
hazzik
  • 13,019
  • 9
  • 47
  • 86