0

How do I make RadioButton required field when textbox is not empty? I have a text box and couple of radiobuttons , if textbox has value I wanted make radiobutton required field.

CoolArchTek
  • 3,729
  • 12
  • 47
  • 76
  • Question is how do I force radio button if textbox not empty [RequiredIf("SomeTextBox", "what to say here" , ErrorMessage = "Message")] – CoolArchTek Jun 10 '16 at 20:57

1 Answers1

0

You can add custom validation rules to any model in ASP.NET MVC. Here's an example:

public class CoolArchTekModel : IValidatableObject {
    public string SomeTextBox { get; set; }
    public bool SomeRadioButton { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) {
        if (string.IsNullOrEmpty(this.SomeTextBox) && !this.SomeRadioButton) {
            yield return new ValidationResult("The radio button is required if the text box is empty!");
        }
    }
}
Justin Helgerson
  • 24,900
  • 17
  • 97
  • 124