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

  • Home
  • SEARCH
  • 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 6058617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:37:08+00:00 2026-05-23T08:37:08+00:00

My goal is to create a validated IP address field that will be validated

  • 0

My goal is to create a validated IP address field that will be validated against an IP address regular express or an dynamic DNS address. So basically if the user enters an valid IP address OR a valid dynamic DNS address the client will allow it to go through. I have found articles showing how to make custom fields and how to multiple attributes on a data field but nothing about how to run them as an OR condition.

Here is my code for the custom IP and Domain Name attributes:

    public class IPAddressAttribute : RegularExpressionAttribute
    {
        public IPAddressAttribute()
            : base(@"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$")
        { }
    }
    public class DomainNameAttribute : RegularExpressionAttribute
    {
        public DomainNameAttribute()
            : base(@"^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$")
        { }
    }

And here is my code for the field:

[Required(ErrorMessage="Wan IP is required")]
[DisplayName("Wan IP")]
[IPAddress(ErrorMessage="Enter a valid IP")]
[DomainName(ErrorMessage="Enter a valid Domain Name")]
public virtual string wan_ip { get; set; }

This code currently checks on both attributes. When I enter a valid IP address it comes back saying that I need to enter in a valid domain name, which is understandable because it’s trying to validate on both of the attributes. Because they are mutually exclusive they can never both be satisfied.

Any suggestions will be greatly appreciated!

Thanks!

Matthew

  • 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-23T08:37:08+00:00Added an answer on May 23, 2026 at 8:37 am

    You could create a custom validation attribute, and use the two existing ones inside it. There’s an article on MSDN on creating custom attributes.

    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
    sealed public class CustomAttribute : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            IPAddressAttribute a1 = new IPAddressAttribute();
            DomainNameAttribute a2 = new DomainNameAttribute();
            return a1.IsValid(value) || a2.IsValid(value);
        }    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My goal is to create a version of the app that will expire after
my goal is to write a stored proc that can collect all field values
I am developing the user management portion of a website that will host a
My goal is to create a generic function that selects a value in a
Goal I want to create a web app with a horizontal GUI bar that
My goal is to create a custom TextBlock control that has a new dependency
My goal is to create (or use existing) an InputStream implementation (say, MergeInputStream) that
My goal is to create a map of maps so that I can retrieve
My goal is to create report in WPF using dev express. I want to
My end goal is to create a firefox extension that inserts an HTML button

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.