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 6773943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:43:41+00:00 2026-05-26T15:43:41+00:00

I am trying to validate a phone number which is contained over two fields.

  • 0

I am trying to validate a phone number which is contained over two fields.

The first field displays the area code and the other field displays the remaining phone number digits.

The requirements are:

  1. They are both required.
  2. They must be numeric.
  3. The area code field must be a maximum of 6 digits and the phone number field must be a maximum of 10 digits.

A single message is required to be displayed for both fields. For example, if the area code was missing but the telephone number was provided then a single message should be displayed after both fields stating “Please enter a telephone number.”.

The model I currently have is similar to:

public class Customer
{
    //...other fields here

    public string AreaCode { get; set; }

    public string PhoneNumber { get; set; }
}

I cannot implement the telephone as a single field with a regular expression or simlar.

There is a similar question to this here (and also kind of similar to here) where the answer recommends creating a custom validation attribute (in this case named MultiFieldRequired) to specify the names of the fields which are required in the attribute. The validation attribute then uses reflection to check the values of the other properties to report if there is an error.

For example, in the case of the above model I believe I should have:

public class Customer
{
    //...other fields here

    public string AreaCode { get; set; }

    [MultiFieldRequired("AreaCode", "PhoneNumber", ErrorMessage="Please enter a phone number")]
    public string PhoneNumber { get; set; }
}

However, I have come across a small problem with the provided solution. The error returned by the custom validation attribute is only reported against the field which has the data annotation (PhoneNumber is the model above). Therefore, if the AreaCode is not entered by the user but the PhoneNumber is supplied, the error is reported against the PhoneNumber field and not the AreaCode. Additionally, if you are using the css supplied with the visual studio Internet project the supplied PhoneNumber field will be displayed in red whilst the invalid area code is displayed as white (not showing an error) so this looks a little odd.

Does anyone have a way to correct this colouring behaviour or a way that I can report an error message if either field is in error (using the correct colouring on the fields)?

Thank you in advance.

  • 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-26T15:43:41+00:00Added an answer on May 26, 2026 at 3:43 pm

    Apply the attribute on both and have only one ValidationMessageFor (anyone) and place it where you want to show the message (from the post it seems you want only one error message).

    public class Customer
    {
        //...other fields here
    
        [MultiFieldRequired("AreaCode", "PhoneNumber", ErrorMessage="Please enter a phone number")]
        public string AreaCode { get; set; }
    
        [MultiFieldRequired("AreaCode", "PhoneNumber", ErrorMessage="Please enter a phone number")]
        public string PhoneNumber { get; set; }
    }
    

    View

        <div class="editor-label">
            @Html.LabelFor(model => model.AreaCode)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.AreaCode)
        </div>
    
        <div class="editor-label">
            @Html.LabelFor(model => model.PhoneNumber)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.PhoneNumber)
            @Html.ValidationMessageFor(model => model.PhoneNumber)
        </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to validate my form. In my form is a phone number field.
I'm trying to validate a phone number against a RegEx. I was provided with
I'm trying to validate a phone number without success. When I submit the form
I am trying to validate a (US) phone number with no extra characters in
Hi I am trying to validate a phone number for a cookie via java-script.
I am trying to validate a string as a phone number (digits and certain
I'm trying to validate a field named phone_number with this rules: the first digit
I'm trying to put together a comprehensive regex to validate phone numbers. Ideally it
I'm trying to validate user input, which is just comma separated numbers. I'd like
I'm trying to validate a USA mobile number, since I'm using pre-built javascript validation

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.