EmailValidator using apache.commons.validator does not correctly validate email

3

index.xhtml file where I specify the tag When I execute the application and enter the data and a correct email and click on save the validator tells me that it is an invalid format

When I do not enter email (which should not be) the validator does not detect it and goes to the next data confirmation window.

Someone who has had this problem?

    
asked by harpazo 16.01.2016 в 02:48
source

3 answers

2

You can create your own validation using regular expressions. (Not recommended)

This would be a simple validation of an email type [email protected]

@FacesValidator(value = "emailValidator")
public class EmailValidator implements Validator{

@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
    Pattern pattern = Pattern.compile("\[email protected]+\w+\.\w+");
    Matcher matcher = pattern.matcher((CharSequence) value);
    HtmlInputText htmlInputText = (HtmlInputText) component;
    String label;

    if (htmlInputText.getLabel() == null || htmlInputText.getLabel().trim().equals("")) {
        label = htmlInputText.getId();
    } else {
        label = htmlInputText.getLabel();
    }

    if (!matcher.matches()) {
        FacesMessage facesMessage = new FacesMessage(label + ": not a valid email address");
        throw new ValidatorException(facesMessage);
    }
}
}

And on the page something like this:

<h:outputLabel value="Email Address:" for="email"/>
<h:inputText id="email" label="Email Address" required="true" value="#{registrationBean.email}">
    <f:validator validatorId="emailValidator"/>
</h:inputText>
<h:message for="email"/>
    
answered by 20.01.2016 в 00:58
1

isEmpty

public static boolean isEmpty(String str)
comprueba si un String is empty ("") o null.

 StringUtils.isEmpty(null)      = true
 StringUtils.isEmpty("")        = true
 StringUtils.isEmpty(" ")       = false
 StringUtils.isEmpty("texto")   = false
 StringUtils.isEmpty("texto")   = false

isNotEmpty

public static boolean isNotEmpty(String str)

 StringUtils.isNotEmpty(null)      = false
 StringUtils.isNotEmpty("")        = false
 StringUtils.isNotEmpty(" ")       = true
 StringUtils.isNotEmpty("texto")   = true
 StringUtils.isNotEmpty("texto")   = true

.

You put:

if(!StringUtils.isEmpty(email){

This makes that if no esta vacio enters the if, and applies what is inside, if I do not understand wrong, this is what you complain about, I think you could solve it using for example:

if(StringUtils.isEmpty(email){
    
answered by 16.01.2016 в 17:25
0

You can also do validation at the bean level once (since it is an object to be persisted).

class User {
    @Id @GeneratedValue
    private long id;
    @Column(nullable=false, unique=true)
    private String username;
    @Column(nullable=false, length=16)
    @Size(min=4, max=16, message="La contraseña debe estar entre 4 y 16 dígitos")
    @Column(nullable=false, unique=true)
    @NotEmpty @Email // proveída por hibernate validator
    private String email;

    // constructores, getters y setters
}

In the view:

<h:inputText id="email" label="Email address" value="#{registrationBean.user.email}" required="true" />
<h:message for="email" errorStyle="color: red" infoStyle="color: blue" />
    
answered by 21.01.2016 в 00:16