wood_menu
Follow me on twitter

php

Regular expressions : Email validation

Working on various web projects, there's a very well known problem : find a good regular expression (regexp) to check the validity of user submitted email addresses.

This website has compiled various regular expressions which try to resolve this problem. The editor's idea is great, using a set of valid/invalid emails, and a simple unit test, he can provide a good comparison of some of the most used regexps.

His philosophy is great : "It's better to accept a few invalid addresses than reject any valid ones, so I'm looking for 0 false-positives and as few false-negatives as possible."
But I've noticed 2 problems :

  • His "best" regexp doesn't work in JavaScript (JS doesn't support advanced features like negative lookbehind ...)
  • The method used to validate IP addresses is not correct (doesn't take care of 0-255 range)

So i've decided to improve an existant one, adding an other test criteria : also check the "real" validity of the IP address. The following work is based on the G. Arluison's improved version of Warren Gaebel's regex.

Here are my solutions :


/^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9]([-a-z0-9_]?[a-z0-9])*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z]{2})|([1]?\d{1,2}|2[0-4]{1}\d{1}|25[0-5]{1})(\.([1]?\d{1,2}|2[0-4]{1}\d{1}|25[0-5]{1})){3})(:[0-9]{1,5})?$/i

This one works very well (found 18/18 valid mails + deep IP address check, and found 19/20 invalid mails - there is a problem checking global length)

There's just a small problem, each time a new TLD > 2 chars will be added, you'll need to append it to the list in the regex, if you want a more generic solution, you can use this variant (note that this version will not check if the TLD really exists) :

/^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9]([-a-z0-9_]?[a-z0-9])*(\.[-a-z0-9_]+)*\.([a-z]{2,6})|([1]?\d{1,2}|2[0-4]{1}\d{1}|25[0-5]{1})(\.([1]?\d{1,2}|2[0-4]{1}\d{1}|25[0-5]{1})){3})(:[0-9]{1,5})?$/i

Those 2 solutions should be usable in all languages providing PCRE (Perl Compatible Regular Expressions), on server & client side (such as Javascript, PHP, Perl, Python, Ruby etc...)

OpenID : How to setup your own identity server using SimpleID

OpenID is a way to provide a single digital identity across the Internet. Instead of creating multiple accounts, remember the login/password couples on all the website you visit, you can use a single OpenID identity if this type of authentication is supported by the target website.

An OpenID is in the form of a unique URL, and is authenticated by the user's 'OpenID provider' (that is, the entity hosting their OpenID URL).The OpenID protocol does not rely on a central authority to authenticate a user's identity. Since neither the OpenID protocol nor Web sites requiring identification may mandate a specific type of authentication, non-standard forms of authentication can be used, such as smart cards, biometrics, or ordinary passwords.

While you can create an identity on various providers websites (it's possible you already have one see Get an Openid) you may want to host your own identity server.

There are a lot of identities servers, in various languages (PHP, Ruby, Python, Java...) i've decided to use SimpleID a lightweight PHP-based solution which doesn't rely on any database. Here is an overview of these features :

  • Support for OpenID 1.1 and 2.0
  • Support for Simple Registration Extension 1.0 and 1.1 draft
  • Multiple identities support

 

Installation

Start by downloading the archive at Sourceforge, extract it then move the cache, identities, www folders to your webserver.
Configure your Web Server (apache, lighttpd...) by adding a new virtualhost pointing to the "www" folder. For standard use, you don't need to setup any rewrite rule or anything else.
Rename config.default.inc to config.inc, then edit this file to put correct paths for the different needed folders
 

Add an Identity

Create an identity file called "yournickname.identity" in the identities folder, your apache/lighttpd user must have read access to this file.

identity=http://vhost.yourdomain.tld
pass=3408cad97ec7f9c09775da84048ecc0
[sreg]
nickname=your_nickname
email=yourmail@domain.tld
administrator=1
fullname=John Doe
dob=1957-01-02
gender=M
postcode=1234
country=ch
language=en
timezone=Europe/Zurich

As for the "pass" line, you have to put the MD5 Hash of your password.

That's all, you can now login to SimpleID using your new identity, and use this identity to login in various websites.

Tags

Flickr Random images

IMAG0019

About the author

photoAlexandre DE DOMMELIN

Geneva - Switzerland