简体   繁体   English

仅允许特定的电子邮件地址

[英]allowing only a particular email address

I am working on an MVC project and i need to verify the email address for a particular group. 我正在开发一个MVC项目,我需要验证特定组的电子邮件地址。 For instance only the people with emailID@stanford.edu can register.I did try data annotations but it only verifies the email address, not a substring. 例如,只有使用emailID@stanford.edu的人才能注册。我确实尝试过数据注释,但它只验证电子邮件地址,而不是子字符串。 How can i check if the email address consists of @stanford.edu ? 如何检查电子邮件地址是否包含@ stanford.edu? Thank you in advance. 先感谢您。

So, based on the suggestion i tried this one, however i get error during build that: no suitable method found to override. 所以,基于我尝试过这个建议,但是我在构建过程中遇到错误:找不到合适的方法来覆盖。 I tried this. 我试过这个。

/*
 * Attribute to validate schools
 */
public class ValidateSchoolAttribute : ValidationAttribute
{
    public string stanfordEmail { get; set; }
    public string harvardEmail { get; set; }

    public override bool IsValid(String value)
    {
        if (value == null){
            return true;
        }

        char[] delimiterCharacter = { '@' };
        String text = value;

        String[] words = text.Split(delimiterCharacter);

        if( words.Equals("stanford.edu") ||words.Equals("harvard.edu")  ){
               return true;
        }
        else{
              return false;
        }

    }
}

You could use a [RegularExpression] attribute 您可以使用[RegularExpression]属性

[RegularExpression(@".*@(harvard|stanford)\.edu")]
[EmailAddress]
public string Email { get; set; }

you should make custom validation attribute. 你应该制作自定义验证属性。

something like this 这样的事情

public class OnlyStanfordEmailAttribute: ValidationAttribute

and then override IsValid function. 然后覆盖IsValid函数。

if you want client side validation too you can follow this tutorial 如果您也想要客户端验证,您可以按照本教程进行操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM