views:

1372

answers:

4

What's the best practice to validate an e-mail address (e.g. from an user input field) in Android? org.apache.commons.validator.routines.EmailValidator doesn't seem to be available. Are there any other libraries doing this which are included in Android already or would I have to use RegExp?

+3  A: 

Don't use a reg-ex.

Apparently the following is a reg-ex that correctly validates most e-mails addresses that conform to RFC 2822, (and will still fail on things like "[email protected]", as will org.apache.commons.validator.routines.EmailValidator)

(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

Possibly the easiest way to validate an e-mail to just send a confirmation e-mail to the address provided and it it bounces then it's not valid.

If you want to perform some basic checks you could just check that it's in the form *@*

If you have some business logic specific validation then you could perform that using a regex, e.g. must be a gmail.com account or something.

Glen
A: 

I had the same problem in a work about a month before. Here's a nice regular expression based Javascript funcion.

//E-mail address validator. 
function checkEmail(inputMail)
    { 
    var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
    if(pattern.test(inputMail)){ return true; } else { return false; }
    }

I executed a verify(); function on the submission of a form, like this:

<form name="imageForm" method="post" action="upload.php" enctype="multipart/form-data" onsubmit="return verify();">

If verify(); function returned false, the form did not posted.

p.s.: I have no idea what android is, but i saw a java tag near your question.

Geri
@Geri, Android is Google's OS for mobile phones (or for our American friends, cell phones)
Glen
:) See, I just felt answery...
Geri
Whoa, there's a huge Android banner in the title of the question...
Geri
A: 

Note that most of the regular expressions are not valid for international domain names (IDN) and new top level domains like .mobi or .info (if you check for country codes or .org, .com, .gov and so on).

A valid check should separate the local part (before the at-sign) and the domain part. You should also consider the max length of the local part and domain (in sum 255 chars including the at-sign).

The best approach is to transform the address in an IDN compatible format (if required), validate the local part (RFC), check the length of the address and the check the availability of the domain (DNS MX lookup) or simply send an email.

Thorsten
A: 

Hi,

I have coded a small solution for the lack of a validation mechanism. You can find this at Android form validation. For the e-mail validation itself you can use the expression validator and add the expression yourself. There are many examples of such an e-mail expression out there.

Kind regards, http://nl.linkedin.com/in/marcdekwant

Jaavaaan