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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:05:56+00:00 2026-05-22T17:05:56+00:00

Howdy, I have what should be a simple question. I have a set of

  • 0

Howdy, I have what should be a simple question. I have a set of validations that use System.CompontentModel.DataAnnotations . I have some validations that are specific to certain view models, so I’m comfortable with having the validation code in the same file as my models (as in the default AccountModels.cs file that ships with MVC2). But I have some common validations that apply to several models as well (valid email address format for example). When I cut/paste that validation to the second model that needs it, of course I get a duplicate definition error because they’re in the same namespace (projectName.Models). So I thought of removing the common validations to a separate class within the namespace, expecting that all of my view models would be able to access the validations from there. Unexpectedly, the validations are no longer accessible. I’ve verified that they are still in the same namespace, and they are all public. I wouldn’t expect that I would have to have any specific reference to them (tried adding using statement for the same namespace, but that didn’t resolve it, and via the add references dialog, a project can’t reference itself (makes sense).

So any idea why public validations that have simply been moved to another file in the same namespace aren’t visible to my models?

CommonValidations.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;

namespace ProjectName.Models
{
    public class CommonValidations
    {
        [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true, Inherited = true)]
        public sealed class EmailFormatValidAttribute : ValidationAttribute
        {
            public override bool IsValid(object value)
            {
                if (value != null)
                {
                    var expression = @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$";
                    return Regex.IsMatch(value.ToString(), expression);
                }
                else
                {
                    return false;
                }
            }
        }
    }
}

And here’s the code that I want to use the validation from:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using Growums.Models;

namespace ProjectName.Models
{
    public class PrivacyModel
    {
        [Required(ErrorMessage="Required")]
        [EmailFormatValid(ErrorMessage="Invalid Email")]
        public string Email { get; set; }
    }
}
  • 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-22T17:05:57+00:00Added an answer on May 22, 2026 at 5:05 pm

    You have declared EmailFormatValidAttribute as a subclass to CommonValidations. As such you need to reference it like CommonValidations.EmailFormatValidAttribute. Or alternatively move the EmailFormatValidAttribute class out of the CommonValidations class.

    This should work:

    [CommonValidations.EmailFormatValid(ErrorMessage="Invalid Email")]
    public string Email { get; set; }
    

    By the way, you can simplify your class as follows:

    public class EmailFormatValidAttribute : RegularExpressionAttribute
    {            
       public EmailFormatValidAttribute() : 
          base(@"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$")
       {}
    }
    

    Also, take a look at this: Data Annotations Extensions. It’s a great DataAnnotations library which has already the most common validations included in it.

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

Sidebar

Related Questions

Alrighty then, howdy first off; quick question I have a form that has multiple
Howdy. I'm working on migrating an internal system to Django and have run into
Howdy. It's me again, and It's jQuery again. I have somthing like that: http://misiur.com/small/
I have a simple message box in a WPF application that is launched as
Simple quick question.... I have the following link html: <a href=http://www.site.com/ onmouseover= /> I
Howdy all, I have a weird situation. I have a C++ code that overloads
Howdy, codeboys and codegirls! I have came across a simple problem with seemingly easy
Howdy, I have a DataRow pulled out of a DataTable from a DataSet. I
Howdy... here on my local LAN, I have a Windows Server 2k8 box with
What I want is, I think, relatively simple: > Bin = <<Hello.world.howdy?>>. > split(Bin,

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.