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.