1

The [Email] attribute in Asp.Net MVC4 does not allow white space on the client side, does anyone know how i can allow something like " myemail@email.com " to pass through the unobtrusive java script generated by the property decorators?

When it hits Http Post, i can trim() the model.Email before doing anything with it. I just need to get past client validation.

I'm guessing any valid email regex will work if i tack on \s* at the start and end of the pattern?

Thanks!

Jerrold
  • 1,534
  • 6
  • 25
  • 43

2 Answers2

3

You can use jQuery to trim the textbox value. Something like:

$("#textboxId").on("change", function () {
   $(this).val($.trim($(this).val()));
}
Dmitry S.
  • 8,373
  • 2
  • 39
  • 49
1

Ok i guess i shouldved tested it before posting, but yes you can tack on a \s* so that ignores whitespace..heres the regex i ended up using

    [RegularExpression(@"^(\s*[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9}\s*)$")]
Jerrold
  • 1,534
  • 6
  • 25
  • 43
  • 1
    This is [not a valid regex for emails](http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address). Or, more to the point, it is extremely simplistic and will match only a tiny subset of valid email addresses. – doppelgreener May 26 '13 at 23:17