3



I have input for email:

<h:inputText value="#{registrationBean.email}" id="email" label="#{msgs.email}">
                        <f:validateLength minimum="5" maximum="50" />
                        <f:validateRegex
                                pattern="^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$" />
                    </h:inputText>
                    <h:message for="email"/>

So, how I can change message, when validateRegex not matched? Now message is:

Regex pattern of '^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$' not matched

But I want something like „Incorrect e-mail…”
Thanks.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user2783755
  • 578
  • 2
  • 10
  • 26
  • 2
    Regardless of the question, since 2010, non-latin characters are [allowed](http://stackoverflow.com/questions/7875108/email-validation-using-regular-expression-in-jsf-2-primefaces/12824163#12824163) in internet domain names (and thus also email addresses). In other words, your regex is outdated. – BalusC Mar 23 '14 at 11:52
  • See answer for question [jsf multiple custom validator messages for regex](http://stackoverflow.com/questions/21464653/jsf-multiple-custom-validator-messages-for-regex/21465934#21465934). It is about email validation. – Vasil Lukach Mar 23 '14 at 20:38

2 Answers2

25

You can use the validatorMessage attribute of inputText. In your case you can use this:

<h:inputText value="#{registrationBean.email}" id="email" label="#{msgs.email}" validatorMessage="Incorrect e-mail…">
    <f:validateLength minimum="5" maximum="50" />
    <f:validateRegex pattern="^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$" />
</h:inputText>
<h:message for="email"/>
Luís Cruz
  • 14,780
  • 16
  • 68
  • 100
Koushik Ravulapelli
  • 1,140
  • 11
  • 30
  • `validatorMessage` is being shown if any of the validators fail. The message should only appear for `f:validateRegex` only. I have the same here, that it is being shown for both validators. – Roland Apr 22 '20 at 22:23
3

You can use validator:

public class EmailValidator implements Validator {
    private static Pattern patt = Pattern.compile(Constants.getEmailRegExp());
    @Override
    public void validate(FacesContext arg0, UIComponent comp, Object arg2)
            throws ValidatorException {
        if (arg2 == null || arg2.toString().trim().length() == 0) {
            return;
        }
        if (!patt.matcher(arg2.toString().trim()).matches()) {
            String label = (String) comp.getAttributes().get("label");
            throw new ValidatorException(new FacesMessage(
                FacesMessage.SEVERITY_ERROR, (label == null || label.trim().length() == 0 ? "Email: " : label+": ") +
                "Invalid format", null));
        }
    }
}

Register it via annotation or in faces-config.xml:

<validator>
    <validator-id>email-validator</validator-id>
    <validator-class>path.EmailValidator</validator-class>
</validator>

And use it

<h:inputText id="email" label="#{msg.email}"
    value="#{registrationForm.email}"
    size="40" maxlength="80" required="true">
    <f:converter converterId="lowerCase" />
    <f:validator validatorId="email-validator" />
</h:inputText>
Vasil Lukach
  • 3,658
  • 3
  • 31
  • 40