MVC Form Validation on Multiple Fields

前端 未结 3 1092
夕颜
夕颜 2020-12-01 09:20

How would I go about having multiple textboxes on an MVC 3 form treated as one for the purposes of validation?

It\'s a simple phone number field with one textbox for

3条回答
  •  我在风中等你
    2020-12-01 10:10

    You could handle this by putting IValidatableObject on the model class, and implementing the Validate method.

    It could look something like this:

    public IEnumerable Validate(ValidationContext validationContext)
    {
          if (String.IsNullOrEmpty(_PhonePart1) || String.IsNullOrEmpty(_PhonePart2)
                || String.IsNullOrEmpty(_PhonePart3))
          {
               yield return new ValidationResult("You must enter all " + 
                      "three parts of the number.");
          }
    
    }
    

提交回复
热议问题