RUiHAO
RUiHAO

Reputation: 19

Java email address validation using regular expression

I am trying to set a regular experssion validation of email address input.

I have this java class which does the validation:

public class EmailValidator implements Validator {

@Override
public void validate(FacesContext facesContext, 
UIComponent uIComponent, Object value) throws ValidatorException {

//Get the component's contents and cast it to a String
String enteredEmail = (String)value;

//Set the email pattern string
Pattern p = Pattern.compile(".+@.+\\.[a-z]+");

//Match the given string with the pattern
Matcher m = p.matcher(enteredEmail);

//Check whether match is found
boolean matchFound = m.matches();

if (!matchFound) {
    FacesMessage message = new FacesMessage();
    message.setDetail("Email not valid - The email must be in the format [email protected]");
    message.setSummary("Email not valid - The email must be in the format [email protected]");
    message.setSeverity(FacesMessage.SEVERITY_ERROR);
    throw new ValidatorException(message);
}
}
}

There is a default pattern in this code. However, I am trying to replace it with another pattern instead.

Pattern:

/^(?:(?:(?:[^@,"[]\x5c\x00-\x20\x7f-\xff.]|\x5c(?=[@,"[]\x5c\x00-\x20\x7f-\xff]))(?:[^@,"[]\x5c\x00-\x20\x7f-\xff.]|(?<=\x5c)[@,"[]\x5c\x00-\x20\x7f-\xff]|\x5c(?=[@,"[]\x5c\x00-\x20\x7f-\xff])|.(?=[^.])){1,62}(?:[^@,"[]\x5c\x00-\x20\x7f-\xff.]|(?<=\x5c)[@,"[]\x5c\x00-\x20\x7f-\xff])|[^@,"[]\x5c\x00-\x20\x7f-\xff.]{1,2})|"(?:[^"]|(?<=\x5c)"){1,62}")@(?:(?!.{64})(?:[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9].?|[a-zA-Z0-9].?)+.(?:xn--[a-zA-Z0-9]+|[a-zA-Z]{2,6})|[(?:[0-1]?\d?\d|2[0-4]\d|25[0-5])(?:.(?:[0-1]?\d?\d|2[0-4]\d|25[0-5])){3}])$/

May I know can this pattern be used? Or do I need to put some brackets etc to make it work? I am receiving illegal start of expression errors now.

Upvotes: 0

Views: 3500

Answers (6)

Jack
Jack

Reputation: 21

Try this please, works very fine with me.

String expressionForEmail = "^[\\w\\.-]+@([\\w\\-]+\\.)+[a-zA-Z]{2,4}$";

Upvotes: 0

8vius
8vius

Reputation: 5836

If you are trying to do an email validation this pattern works and is much shorter ^[a-z0-9!#$%&'*+/=?^_{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$

I use java's regex util package (java.util.regex.*) here's the documentation on it and on regular expressions in java in general:

http://java.sun.com/developer/technicalArticles/releases/1.4regex/

Upvotes: 0

Imran Ali
Imran Ali

Reputation: 59

you can use this expression for email validation "[A-Za-z]+@+[A-Za-z]+\.+[A-Za-z]{2,4}+$" it is much more simpler to do that.

Upvotes: -2

Radu Ciopraga
Radu Ciopraga

Reputation: 318

You could also use the EmailValidator from Apache Commons Validator.

Upvotes: 5

fgysin
fgysin

Reputation: 11913

You can use that RegEx (or any you want, at that) but keep in mind to excape the backslash \ in java strings (by entering two \\).

Here is a page that lets you transform regex strings into Java strings, i.e. it does all the escaping of weird characters for you.... (Page seems to be down right now, but it was working yesterday, so just check in later...)

Upvotes: 0

Jeffrey
Jeffrey

Reputation: 44808

I'm sure that regex can be used, but you'll have to properly insert it into your code. However, the best way to check if an email is valid is to send it an email. Here's a good site for email verification with regex, and of course you can always search this site for the many similar questions.

Upvotes: 0

Related Questions