6

I am using .NET for my email validation:

    [Display(Name = "Email address")]
    [Required(ErrorMessage = "The email address is required")]
    [EmailAddress(ErrorMessage = "Invalid Email Address")]

But if I copy paste an email address like bc@good.com ( notice the empty space at the end of .com it says it is invalid. How can I tell it to ignore that empty space for validation?

I couldn't type empty space in SO editor, imagine there are empty spaces after .com

  • 1
    If you include the space, you really do have an invalid e-mail address. Instead of ignoring it during validation, can you not get rid of it before validation? –  Aug 03 '16 at 14:26
  • 2
    Here is the code for EmailAddressAttribute http://referencesource.microsoft.com/#System.ComponentModel.DataAnnotations/DataAnnotations/EmailAddressAttribute.cs. You could write your own version based on this but ignoring spaces. – Andy Nichols Aug 03 '16 at 14:33
  • Ideally if you want to allow trailing whitespaces, you should be trimming those on the client side before performing any validation. You could write a JavaScript function to do that. However, I am not sure if this function would interfere with the client side unobtrusive jQuery validation, if you have that switched on. – Bhushan Shah Aug 03 '16 at 15:36

1 Answers1

6

You could use the regex validation Attribute and use some Regex that allows for spaces at the beginning and end. An example is given below :

^ *([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,6}) *$

However, this regex is a sample only and I recommend you visit This site or the SO post mentioned in the comments below to find a regex that is acceptable to your needs and then use the [Space]* at either end to allow unlimited spaces

Rich Linnell
  • 973
  • 5
  • 14
  • I want to enter my email address as bob+smith@foo.net - this regex will not allow it. Also it will reject many of the TLD found in https://en.wikipedia.org/wiki/List_of_Internet_top-level_domains#ICANN-era_generic_top-level_domains such as .marketing by limiting the TLD to be 2-5 characters long. Also http://stackoverflow.com/questions/3844431/are-email-addresses-allowed-to-contain-non-alphanumeric-characters may be of interest. – Chris Aug 03 '16 at 14:42
  • This is a basic email validation regex. There are many others available via an internet search that will cater for such situations. The important take aways relevant to the question are 1> use the Regex Validator 2> use a recognised email regex string 3> modify it with '[space]*' at the start and end to allow for the spaces. – Rich Linnell Aug 03 '16 at 14:48
  • 1
    My problem with the answer is that by putting that regex as part of your answer to the question then people will see it and assume it *is* an appropriate regex to use. People come to SO and just copy and paste the code all the time. They aren't going to see this answer and then say "And now to find a regular expression for the email part". If you want to make it clear that the regex in your answer shouldn't be considered a suitable one for actually validating email addresses then that would be great but as it reads to me it feels like the answer is suggesting it is a complete solution. – Chris Aug 03 '16 at 14:54