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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:11:11+00:00 2026-05-15T22:11:11+00:00

Could someone help me with this issue. I’m trying to figure out how to

  • 0

Could someone help me with this issue. I’m trying to figure out how to check two values on a form, one of the two items has to be filled in. How do I do a check to ensure one or both of the items have been entered?

I’m using viewmodels in ASP.NET MVC 2.

Here’s a little snip of code:

The view:

Email: <%=Html.TextBoxFor(x => x.Email)%>
Telephone: <%=Html.TextBoxFor(x => x.TelephoneNumber)%>

The viewmodel:

    [Email(ErrorMessage = "Please Enter a Valid Email Address")]
    public string Email { get; set; }

    [DisplayName("Telephone Number")]
    public string TelephoneNumber { get; set; }

I want either of these details to be provided.

Thanks for any pointers.

  • 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-15T22:11:11+00:00Added an answer on May 15, 2026 at 10:11 pm

    You can probably do this in much the same way as the PropertiesMustMatch attribute that comes as part of the File->New->ASP.NET MVC 2 Web Application.

    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
    public sealed class EitherOrAttribute : ValidationAttribute
    {
        private const string _defaultErrorMessage = "Either '{0}' or '{1}' must have a value.";
        private readonly object _typeId = new object();
    
        public EitherOrAttribute(string primaryProperty, string secondaryProperty)
            : base(_defaultErrorMessage)
        {
            PrimaryProperty = primaryProperty;
            SecondaryProperty = secondaryProperty;
        }
    
        public string PrimaryProperty { get; private set; }
        public string SecondaryProperty { get; private set; }
    
        public override object TypeId
        {
            get
            {
                return _typeId;
            }
        }
    
        public override string FormatErrorMessage(string name)
        {
            return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,
                PrimaryProperty, SecondaryProperty);
        }
    
        public override bool IsValid(object value)
        {
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
            object primaryValue = properties.Find(PrimaryProperty, true /* ignoreCase */).GetValue(value);
            object secondaryValue = properties.Find(SecondaryProperty, true /* ignoreCase */).GetValue(value);
            return primaryValue != null || secondaryValue != null;
        }
    }
    

    The key part of this function is the IsValid function that determines if one of the two parameters has a value.

    Unlike normal Property-based attributes, this is applied to the class level and can be used like so:

    [EitherOr("Email", "TelephoneNumber")]
    public class ExampleViewModel
    {
        [Email(ErrorMessage = "Please Enter a Valid Email Address")]
        public string Email { get; set; }
    
        [DisplayName("Telephone Number")]
        public string TelephoneNumber { get; set; }
    }
    

    You should be able to add as many as these as you need per form, but if you want to force them to enter a value into one of more than two boxes (Email, Telephone or Fax for example), then you would probably be best changing the input to be more an array of values and parse it that way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm kinda stuck with this one so I hoped someone could help me. I
I was hoping someone could help me in resolving this issue. I have 3
I would be very grateful if someone could help me break through this issue
could someone help me on this problem, i want to access facebook API through
Could someone help me on this, I have created simple web services using axis2
Could someone help me understand the primitive accessors with this example : i don't
Could someone please help explain why I can't get this to work? I properly
I hope someone could help me on this. I want to add a month
I was wondering if someone could help me with this. I have defined my
I would be very grateful if someone could help me with this. I have

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.