-1

Below is to validate valid email. But I want to add that if user enter - only. that will valid. How can I improve the code below so i can validate valid email or enter dash(-) only.

RegularExpressionValidator
ID="RegularExpressionValidatorControlCompanyEmail" runat="server"
Display="Dynamic" ControlToValidate="TextBoxCompEmail"
ValidationExpression='[^\s()\[\]<>\\,:;@"]*@\w+([-.]\w+)*\.\w+([\-.]\w+)*([,;]\s*[^\s()\[\]<>\\,:;@"]*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*'
ErrorMessage="<%$
Resources:LanguagePack.ProfileManagement,InvalidEmail %>"
ValidationGroup="Delete" Enabled="true"
SamWhan
  • 8,296
  • 1
  • 18
  • 45
Lejen
  • 3
  • 2
  • Why is leaving the field blank not a valid thing to do? Just don't add a requiredfieldvalidator. IF you want to set the blank email to "-" in server side you can do that there. – Esko Dec 22 '17 at 12:17
  • I don't want to leave the field blank. I still need to validate the email @Esko – Lejen Dec 22 '17 at 12:20
  • I will be validated if the user enters something in the field since you have RegulaExpressionValidator. You want the field to be mandatory but still allow "-"? If so, this is purely regular expression question. – Esko Dec 22 '17 at 12:21
  • 1
    @Lejen This question makes no sense. If the email is optional, then **let it be an optional field**. If the email is required, then **do not allow `-`**. – Tom Lord Dec 22 '17 at 12:35
  • On a side note, I do not advocate using a complex regex to validate email addresses. Not only is is *very complicated* to write a fully comprehensive regex, but there is still no guarantee that the email *is* valid - e.g. `not-a-real-email@fake-domain.com` will pass your test! If you really need to ensure the email is correct, then **send a confirmation email**. – Tom Lord Dec 22 '17 at 12:37
  • @PanagiotisKanavos This question has nothing to do with Asp.Net MVC, why did you mark is as duplicate of that question? – Esko Dec 22 '17 at 13:24
  • @Esko because the *duplicate answer* has nothing to do with ASP.NET MVC either. The DataAnnotation validation attributes work with any project. And this *is* an ASP.NET question after all. Check for example [What's new in ASP.NET 4.5](https://learn.microsoft.com/en-us/aspnet/web-forms/overview/getting-started/hands-on-labs/whats-new-in-web-forms-in-aspnet-45) – Panagiotis Kanavos Dec 22 '17 at 13:39

2 Answers2

-2

You can use the System.Net.Mail.MailAddress.

Write something like this

var mail = new System.Net.Mail.MailAddress("test@test.de");

if the E-Mail has a invalid syntax a exception will be thrown.

Torben
  • 438
  • 1
  • 7
  • 22
  • 1
    The op did not ask about this. – Esko Dec 22 '17 at 12:18
  • Email addresses can contain the most unusual patterns and characters. It's not possible to write a regex that allows all valid email addresses. The OP is trying to validate only a *subset* the valid ones – Panagiotis Kanavos Dec 22 '17 at 12:38
  • Besides, there's already a validation attribute for emails, [EmailAddressAttribute](https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.emailaddressattribute(v=vs.110).aspx) – Panagiotis Kanavos Dec 22 '17 at 12:46
-3

I got the answer. Regex can have OR and If-Else Condition. So I use Or condition to validate either - OR valid email.

ValidationExpression=' (^-$)|([^\s()[]<>\,:;@"]@\w+([-.]\w+).\w+([-.]\w+)([,;]\s[^\s()[]<>\,:;@"]@\w+([-.]\w+).\w+([-.]\w+)))'

And I found a great site to test Regex

https://regex101.com

Lejen
  • 3
  • 2
  • 2
    This is the correct answer to the **wrong question**. See my comment above. This is a terrible design to "allow `-`" instead of just allowing blank. – Tom Lord Dec 22 '17 at 12:39
  • Good luck debugging this. In any case, email addresses can't be validated with a regex. If you want the field to be optional, make it optional, ie make it accept an empty string. Or add a checkbox. – Panagiotis Kanavos Dec 22 '17 at 12:40
  • If you wan to validate that an *internal* email address is valid, look it up. If you want to make things easier for the users, allow autocompletion that actually checks Active Directory, Exchange or whatever you use for email. If you want to *really validate* the address you'll have to do so anyway, rather than allow someone to send to a non-existent address – Panagiotis Kanavos Dec 22 '17 at 12:43