0

I need a very general and loose regular expression for an email.

It just needs to validate that there is an @ in the email string.

For example:

  • a@a = valid
  • a.@___asdsadc = valid
  • aaaaa@sdsdc.com = valid
  • ssdsadsadassd = invalid

I currently got up to here:

public class EmailAttribute : RegularExpressionAttribute, IClientValidatable
{
    public EmailAttribute()
        //: base("[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[A-Za-z0-9](?:[A-Za-z0-9-_]*[A-Za-z0-9])?\\.)+[A-Za-z0-9](?:[A-Za-z0-9-_]*[A-Za-z0-9])?")
        : base(".*@.*")
    {
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var errorMessage = FormatErrorMessage(metadata.GetDisplayName());

        yield return new EmailValidationRule(errorMessage);
    }
}

public class EmailValidationRule : ModelClientValidationRule
{
    public EmailValidationRule(string errorMessage)
    {
        ErrorMessage = errorMessage;
        ValidationType = "email";
    }
}

So the first one is too tight, and the 2nd one does not seem to return the correct value - I am still being told my string is invalid.

Is it because my regex is wrong? I suspect this validator might not be the same as the rest, as the validatorType is "email", and thus it has worked without me needing to register it as an unobtrusive validator.

It doesn't have to be a proper regex, it just has to be able to validate anything as long as it has the @ symbol on it.

I've used this site to validate my regex: http://gskinner.com/RegExr/ It says my test strings are valid, but this attribute does not pick up changes.

  • I understand that you have very loose requirements (i.e. it doesn't have to be an actual email address) but you will have touched a raw nerve here on SO. This is one of *those questions*. Good luck. – Joe Mar 08 '13 at 09:52
  • To mitigate what Joe is talking about, I'd suggest changing the question to *not* specifically being about emails, but just strings with @s in them. =) – J. Steen Mar 08 '13 at 09:53
  • 1
    I have read around, and found various articles, from Catastrophic Backtracking, to using MailAddress to validate emails. Right now, what I need is to get this regex to work, as this is the requirement. Once it's solved, the unobtrusive validation will take care of my front end validation. Basically, I don't need a proper regex for email, I just need it to work as described above. – Julian Wen Hsi Lee Mar 08 '13 at 09:55
  • @JulianWenHsiLee I believe you should have `.*?@.*` as your regex – Candide Mar 08 '13 at 10:18
  • Thanks @Candide, that worked! I also switched out from using the ASP.Net validator to using a mix of jQuery front end validation, and DataAnnotation back ends for the other attributes such as Required. – Julian Wen Hsi Lee Mar 08 '13 at 10:48
  • 1
    Regex is a little heavyweight if you're just checking for the existence of a single character. Why is `possibleEmail.Contains('@')` no good? – Keith Mar 08 '13 at 13:49
  • It just has to be able to do front end validation, back end isn't quite as important. Therefore I ended up using jQuery instead, to create the activity of a custom validation class. For some reason, the one above was not working. I'd believe that the proper way to would be to write my own adapter, so I can use it as a custom DataAnnotations function, but as stated above, that isn't working, and there are time constraints, so I had to go with something simpler. – Julian Wen Hsi Lee Mar 10 '13 at 22:59

2 Answers2

0

Use this:

^[^@]+@[^@]+$

^ and $ anchor to the beginning and end of the string, requiring the entire string to match. Without the anchors, you could have @@@@foo@bar and the regex would match the foo@bar portion.

[^@]+ matches one or more characters that is not @, ensuring that you only have one @ in the string, and also ensuring that there is at least one character on each side of the @.

  • Same as Ronald, this one works as well, unfortunately I cannot upvote your answer as I do not have at least 15 reputation to do so. Thanks for the reply – Julian Wen Hsi Lee Mar 08 '13 at 10:51
  • @JulianWenHsiLee, you need to use this one if you want to ensure that there is only one `@`. –  Mar 08 '13 at 10:54
  • Thanks for the reply @dan1111, but it does not have to be only one @, it just needs to contain at least one. – Julian Wen Hsi Lee Mar 10 '13 at 22:56
0

I don't understand exactly how your code works and if maybe there your fault lays, but the RegEx pattern should be as simple as ^.*\@{1}.*$ I think @ is a reserved character which you have to escape.

Ronald
  • 16
  • 1
  • This worked as well, unfortunately I cannot upvote your answer as I do not have at least 15 reputation. Thanks for the reply – Julian Wen Hsi Lee Mar 08 '13 at 10:50
  • Note that this will not ensure there is only one `@`. `@{1}` is not doing what you think it is doing. It matches a single `@` character, but doesn't care at all about the rest of the string. This pattern would happily match `@@@@@@@@@@@@`. –  Mar 08 '13 at 10:55
  • @dan1111: you're right. Your answer is better. I would still escape `@` though. – Ronald Mar 08 '13 at 12:56
  • @dan111, that's fine. This is the one I was looking for then if it can have multiple @, but just needs to contain at least one – Julian Wen Hsi Lee Mar 10 '13 at 22:57
  • @JulianWenHsiLee, if that is all you care about then just use this for your regex: `\@`. –  Mar 11 '13 at 13:37