views:

425

answers:

2

Hi all, I'm quite new to this:

I have created a simple form which contains a textbox and button and basically when the email address entered is correct, some results are shown below (this is using a gridview control).

What I am wanting to do is have some sort of email validation for the form - but have the validation placed within the page_load (within the button click) rather than the code behind the page itself. I'm after a simple validation that checks an email has been entered otherwise display a popup and the email format is correct ([email protected]) in C#

+1  A: 

I assume that the details will be displayed only when the buton is clicked ( form submitted) if so why not add a RegularExpression validator and map it to the text box. Then use the following regular expression to validate an email.

\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b

This way it will increase user experience as well. The user does not have to wait for a post back to get the error alert.

Another regular expression for email format validation here.

Shoban
Cheers for that, I'll have a look at them.
If you use the RegularExpressionValidator control, and that field is required by the user, be sure to also use the RequiredFieldValidator as the former control will not fail on blank entries. Using the latter will ensure blank entries are caught.
Ahmad Mageed
+1  A: 

This has been covered here.

Problem is there is no valid regex that covers the whole RFC 5322 grammar. All common regexes (like the one stated by Shoban) are too strict - example: the end part [A-Z]{2,4} that is supposed to cover the top level domain will tag .museum emails as invalid; but there are much more complex examples, like German Umlauts (vowel mutations) that have been allowed not too recently.

Our approach is to check for a superset rather than a subset of allowed emails in validation controls (like the one integrated integrated in Visual Studio that uses \w+([-+.']\w+)@\w+([-.]\w+).\w+([-.]\w+)* ) and deepen the check back on the server (maybe even use a webservice, like this one.

BTW, here is a better regex.

Olaf