0

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.

1
  • What are you using the pattern for? Commented Aug 17, 2011 at 4:58

6 Answers 6

5

You could also use the EmailValidator from Apache Commons Validator.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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...)

Comments

0

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/

2 Comments

so all i have to do is escape all / and \ ?If I escape only "/" it does not work
I modified the regex to work for java, just follow the documentation I gave you and you should be able to do it without a problem.
0

Try this please, works very fine with me.

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

Comments

-2

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.

1 Comment

That regex will not validate all valid email addresses.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.