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 yourname@yourdomain.com");
message.setSummary("Email not valid - The email must be in the format yourname@yourdomain.com");
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|开发者_StackOverflow社区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.
You could also use the EmailValidator from Apache Commons Validator.
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.
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...)
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/
Try this please, works very fine with me.
String expressionForEmail = "^[\\w\\.-]+@([\\w\\-]+\\.)+[a-zA-Z]{2,4}$";
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.
精彩评论