8

I am using the follwoing regular expression

(".+@.+\\.[a-z]+")

Bit it accepts #@#.com as a valid email. What's the pattern I should use?

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
aks
  • 1,849
  • 8
  • 24
  • 21
  • 1
    http://stackoverflow.com/questions/3232/how-far-should-one-take-e-mail-address-validation – Johan May 04 '10 at 05:24
  • 8
    There is no good and realistic regex for email validation. The grammar (specified in RFC 5322) is too complicated for that. http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses/201378#201378 – Amarghosh May 04 '10 at 05:24
  • How big is Yahoo!? They can't get email address syntax checking right, you can't either. http://twitter.com/edent/status/11687784947 – Quentin May 04 '10 at 05:35
  • 2
    This question has to be asked at least once a month or something – hhafez May 04 '10 at 05:54

7 Answers7

29

You should use apache-commons email validator. You can get the jar file from here.

Here is a simple example of how to use it:

import org.apache.commons.validator.routines.EmailValidator;

boolean isValidEmail = EmailValidator.getInstance().isValid(emailAddress);
mR_fr0g
  • 8,462
  • 7
  • 39
  • 54
CoolBeans
  • 20,654
  • 10
  • 86
  • 101
2

Here's a web page that explains that better than I can: http://www.regular-expressions.info/email.html (EDIT: that appears to be a bit out of date since it refers to RFC 2822, which has been superseded by RFC 5322)

And another with an interesting take on the problem of validation: http://www.markussipila.info/pub/emailvalidator.php

Generally the best strategy for validating an email address is to just try sending mail to it.

David Z
  • 128,184
  • 27
  • 255
  • 279
1

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

Mahdi Esmaeili
  • 555
  • 7
  • 11
1

If somebody wants to enter non-existent email address he'll do it whatever format validation you choose.

The only way to check that user owns email he entered is to send confirmation (or activation) link to that address and ask user to click it.

So don't try to make life of your users harder. Checking for presence of @ is good enough.

Konstantin Spirin
  • 20,609
  • 15
  • 72
  • 90
0

I usually use the following one:

([a-zA-Z0-9]+(?:[._+-][a-zA-Z0-9]+)*)@([a-zA-Z0-9]+(?:[.-][a-zA-Z0-9]+)*[.][a-zA-Z]{2,})
sp00m
  • 47,968
  • 31
  • 142
  • 252
0
import java.util.regex.*;

class ValidateEmailPhone{

    public static void main(String args[]){

        //phone no validation starts with 9 and of 10 digit
        System.out.println(Pattern.matches("[9]{1}[0-9]{9}", "9999999999"));

        //email validation
        System.out.println(Pattern.matches("[a-zA-Z0-9]{1,}[@]{1}[a-z]{5,}[.]{1}+[a-z]{3}", "abcd@gmail.com"));
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Nitin Pawar
  • 1,634
  • 19
  • 14
0

This is my regex for email validation:

(([a-zA-Z0-9]+)([\.\-_]?)([a-zA-Z0-9]+)([\.\-_]?)([a-zA-Z0-9]+)?)(@)([a-zA-Z]+.[A-Za-z]+\.?([a-zA-Z0-9]+)\.?([a-zA-Z0-9]+))

For username it allows ".", "_", "-" for separators. After "@" allows only "." and "-". Can be easy modified for more words.