3

I am currently in a situation where we do not want to allow outside emails for one of our applications. The application is developed in Asp.Net MVC. I am aware that on the model side you can do server side validation which you can then use to get front end validation. My problem is that the RegularExpression I use always returns true as if it is only looking for one of those letters not the whole string. How can I make it look for the whole string?

Here is an example code snippet and result.

[Required]
[EmailAddress]
[Display(Name = "Email")]
//I have tried many different variations on the regex
[RegularExpression("{/@gmail}", ErrorMessage = "No Gmail accounts allowed")]
//[RegularExpression("{[/@gmail]}", ErrorMessage = "No Gmail accounts allowed")]
//[RegularExpression("{[/@][g][m][a][i][l]}", ErrorMessage = "No Gmail accounts allowed")]
//[RegularExpression("/@gmail", ErrorMessage = "No Gmail accounts allowed")]

public string Email {get; set;}

This seems to be the result no matter what variation I try on the regex. I also read through all the rules and I didn't see anything for matching a string. Result

Very stumped.... In perl it would be as simple as "s/@gmail//g", is there nothing similar that can be done from the model side?

@PeterB

[RegularExpression("@gmail", ErrorMessage = "Please do not use your gmail account.")]

Result using the gmail format

gregnnylf94
  • 370
  • 3
  • 16
  • I'm not familiar with ASP.net, but do you need to use brackets at all here?? Does `RegularExpression("@gmail"....` not work? – Tom Lord Jan 04 '17 at 14:43
  • I tried this as well but had no luck, it seems to be searching for one of those letters rather than the literal string value. – gregnnylf94 Jan 04 '17 at 14:44
  • Erm... I doubt it... Are you sure you restarted the server/etc after saving something like `[/@gmail]` - which *would* behave like that, due to the special meaning of square brackets in regular expressions? – Tom Lord Jan 04 '17 at 14:46
  • Yes, I am working in visual studio locally, so anytime I stop debugging to make a change, save changes, then debug it restarts the local server – gregnnylf94 Jan 04 '17 at 14:49
  • Tried `[RegularExpression("@gmail", ErrorMessage = "No Gmail accounts allowed")]` yet? – Wiktor Stribiżew Jan 04 '17 at 14:50
  • Without escaping the @? – gregnnylf94 Jan 04 '17 at 14:52
  • Regardless I just tried both ways with the same result.... – gregnnylf94 Jan 04 '17 at 14:53
  • Can you please just do a sanity check here... Delete that validation, or change it to something completely different, and see what validation errors still appear. I am not convinced that the server is picking up the change; or perhaps you are testing on the wrong server. – Tom Lord Jan 04 '17 at 14:53
  • 1
    `[RegularExpression("")]` tells what format is REQUIRED. Not the format that is FORBIDDEN. The ErrorMessage shows when input does not match the REQUIRED format. – Peter B Jan 04 '17 at 14:55
  • @TomLord I did and yes I'm sure I'm just testing it on Visual studios local server so I'm positive I'm not on the wrong server and that it is restarting on each run. – gregnnylf94 Jan 04 '17 at 15:01
  • @PeterB Ah! I had that suspicion. Is there a way to say the opposite? something like [RegularExpression(!"")] or something to that effect" – gregnnylf94 Jan 04 '17 at 15:01
  • @PeterB I thought I tested that because I thought the same thing. So I just tested it with a gmail account with the same error...? – gregnnylf94 Jan 04 '17 at 15:06
  • you need to eliminate the @gmail string from the regex, check this question [link](http://stackoverflow.com/questions/406230/regular-expression-to-match-line-that-doesnt-contain-a-word) – Noor Samara Jan 04 '17 at 15:08
  • @NoorSamara Thank you so much! That was exactly what I needed. Although I don't understand the format... The solution was `^((?!gmail).)*$` – gregnnylf94 Jan 04 '17 at 15:12
  • Great Community thank you for all the help, been stuck on this for a full day now -_- – gregnnylf94 Jan 04 '17 at 15:13
  • 1
    Be careful with that pattern, it will also deny e.g. `dogmaillusion@ok.com` – Peter B Jan 04 '17 at 15:17
  • @PeterB I changed it to `^((?!@gmail).)*$` that should handle that issue. Thanks for the catch. – gregnnylf94 Jan 04 '17 at 15:24

1 Answers1

3

Based on your original understanding, the answer would have been to use:

[RegularExpression("@gmail", ErrorMessage = "No Gmail accounts allowed")]

However, this validation rule actually states that the email must contain "@gmail", not must not contain.

The solution, then, is to use a negative lookahead to negate the pattern - something like:

[RegularExpression("^(?!.*@gmail)", ErrorMessage = "No Gmail accounts allowed")]

This is saying "looking ahead from the start of the string, it cannot contain the pattern: "@gmail"".

Tom Lord
  • 27,404
  • 4
  • 50
  • 77
  • 1
    Also worth noting: You should probably block `@googlemail` too, as that would be a trivial way to bypass your check :) – Tom Lord Jan 04 '17 at 15:20