I confess – we geeks are a strange breed. (Actually, it’s surprising that we are allowed to breed…)
I had an algorithm for testing an e-mail address in Perl, but I just didn’t like it. Wasn’t robust enough for me.
I figured – and I’m sure I’m correct – that this has been a million times by a million people, and it would be for the taking somewhere on the Web.
Well, I found a couple of regexes that were close, but – again – not quite what I was looking for.
So I rolled my own (again…), and it think it’s what I want.
If the e-mail address doesn’t match this mask, invalid address:
/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])\.([a-zA-Z0-9_-]{2,4})/
Update 11/11/03: Improved below...
/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+\.([a-zA-Z]{2,4})$/
Notably, what this does that my other one didn’t is the following:
- Allows periods (dot), hyphens and underscores in first part of address (before @), but does not allow these special characters to be the first character.
- Allows only one @ character (flaw in my last regex)
- Requires 2-4 character domains (.ca, .net, .info). I haven’t checked this out at ICANN, but I think that 2-4 characters is the current upper and lower limits (another flaw in my last regex).
Go ahead, embrace your inner and outer geek…