Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5971441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:31:00+00:00 2026-05-22T20:31:00+00:00

How would I go about having multiple textboxes on an MVC 3 form treated

  • 0

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 area code, one for prefix and one for the last four digits.

There are really two validation requirements:

1) They’re all required.
2) They must all contain integers.

Now this is simple when doing it for individual fields but how can I create the equivalent of an ASP.NET CustomValidator with MVC so that I can validate all three fields as a whole?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T20:31:01+00:00Added an answer on May 22, 2026 at 8:31 pm

    I actually ended up implementing a custom ValidationAttribute to solve this, using the same type of logic presented in CompareAttribute that allows you to use reflection to evaluate the values of other properties. This allowed me to implement this at the property level instead of the model level and also allows for client side validation via unobtrusive javascript:

    public class MultiFieldRequiredAttribute : ValidationAttribute, IClientValidatable
        {
            private readonly string[] _fields;
    
            public MultiFieldRequiredAttribute(string[] fields)
            {
                _fields = fields;
            }
    
            protected override ValidationResult IsValid(object value, ValidationContext validationContext)
            {
                foreach (string field in _fields)
                {
                    PropertyInfo property = validationContext.ObjectType.GetProperty(field);
                    if (property == null)
                        return new ValidationResult(string.Format("Property '{0}' is undefined.", field));
    
                    var fieldValue = property.GetValue(validationContext.ObjectInstance, null);
    
                    if (fieldValue == null || String.IsNullOrEmpty(fieldValue.ToString()))
                        return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));
                }
    
                return null;
            }
    
            public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
            {
                yield return new ModelClientValidationRule
                {
                    ErrorMessage = this.ErrorMessage,
                    ValidationType = "multifield"
                };
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would i go about having my site know lets say IE User Visit's
I would like to serialize and deserialize objects without having to worry about the
I'm wondering how you might go about implementing multiple form actions when submitting a
If there's one thing I've learned about Rails 3 is if I'm having a
This is a question about incrementing one value of a MATLAB array multiple times
Just wondering how you would go about implementing something similar to stackoverflow'd related questions.
Any ideas how I would go about writing a javascript method to insert an
Just a quick question about how you would go about implementing this. I want
I would like to know how I would go about altering the HTML that
I'm wondering how you would go about designing a good permgen space string in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.