Showing posts with label email. Show all posts
Showing posts with label email. Show all posts

Monday, November 22, 2010

Best Validation Expression for Email Address

If you've been playing around with the ASP.NET Regular Expression Validator or any other RegEx tools, then you've more than likely had to check an email addresses for its validity at some point.

Email Address values can range all over the place, which makes them somewhat difficult to correctly validate, especially because you don't want someone to not be able to complete a form if their email address doesn't comply with the regular expression you've chosen, or worse, you don't want someone to input an incorrect email address to abide by your validation expression.

I've been testing various expressions that I've been hunting down across the internet and I've found what I believe the be the best on the simple url: http://www.regular-expressions.info/email.html

This site addresses the RFC 2822 standard and makes some minor tweaks to it, leaving you with two options. The better of which is [a-z0-9!#$%&'*+/=?^_`{}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}comorgnetedugovmilbizinfomobinameaeroasiajobsmuseum)\b

However, the above expression does require constant review, as it specifically spells out the top level domains. As new domains are created, they will need to be added to this list. So, tomorrow if the new top level domain was created .webdev (i.e. rob@digitalplaydoh.webdev) then, you would need to add webdev to this expression to allow for all email addresses to be valid.

Rather than constantly need to check on these top-level domains, I much prefer a tweaked version that allows for a much less strict set of top-level domains, including those that don't exist such as rob@digitalplaydoh.fake. It gives the user the ability to provide a fake email address, but at least I know it's an email address in a proper format.

Therefore, I find this to be the most accurate email address regular expression:
[a-z0-9!#$%&'*+/=?^_`{}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

Thursday, July 1, 2010

Easy Way To Email Errors From Your ASP.Net Site To You

Errors on your site are never good, but it's best when you know about them the second they happen rather than a user telling you- or worse- not telling you.

This is a quick a dirty method I use for emailing myself details about errors when they occur on my sites.

If you don't already have one, add a Global Application Class (Global.asax) to your site. It should come with a sub procedure named Application_Error, but if it doesn't go ahead and create one. In the code I've provided below, I use a custom email class I use to templatize my emails, but you can easily adapt it to any email class you choose.

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when an unhandled error occurs

Dim mm As New MMEmail()
mm.ToRecipients.Add("")
mm.Subject = "[ERROR] " & Request.Url.AbsoluteUri.ToString

Dim ex As Exception = Server.GetLastError().GetBaseException()
Dim body As String = ""

'Get URL where error occured
body &= "[URL]"
body &= "<br/>"
body &= Request.Url.AbsoluteUri.ToString
body &= "<br/>"
body &= "<br/>"

'Brief message of what went wrong and line where error occured in code.
body &= "[MESSAGE]"
body &= "<br/>"
body &= ex.Message.Replace(vbCrLf, "<br/>")
body &= "<br/>"
body &= "<br/>"

body &= "[SOURCE]"
body &= "<br/>"
body &= ex.Source.Replace(vbCrLf, "<br/>")
body &= "<br/>"
body &= "<br/>"

body &= "[STACK TRACE]"
body &= "<br/>"
body &= ex.StackTrace.Replace(vbCrLf, "<br/>")
body &= "<br/>"
body &= "<br/>"

mm.Body = body
mm.Send()
End Sub


Obviously, you'll need to add your email address in and use either the default email class or another class if you'd prefer. You'll need the email to be set up in the web.config - which may be another good topic I could touch on another time. And then, it wouldn't hurt to go intentionally create an error. I made a blank page with the on load function that calls a function s() which doesn't exist:
Public Sub Form_Load() Handles Me.Load
s()
End Sub