简体   繁体   中英

Validation in JSF

I'm trying to implement the Validation of a form but it don't accept when I fill in numbers or characters.

This is my login.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core">

    <h:body>
        <ui:composition template="./templates/template.xhtml">
            <ui:define name="content">
                <p><h:link outcome="/index" value="To Home Page" /></p>
                <h:form>
                    Username: <h:inputText id="username" value="#{user.name}" required="true" requiredMessage="username name is required"><f:validator validatorId="usernameValidator" /></h:inputText><br/>
                    Password: <h:inputText id="password" value="#{user.password}" required="true" requiredMessage="password is required"><f:validator validatorId="passwordValidator" /></h:inputText><br/>
                    Email: <h:inputText id="Email" value="#{user.email}" required="true" requiredMessage="email is required"><f:validator validatorId="emailValidator" /></h:inputText>

                    <h:commandButton id="cBtn2" value="Submit" action="home"/>
                </h:form>

            </ui:define>
        </ui:composition>

    </h:body>
</html>

And my validation class:

package Validation;


import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

public class PasswordValidator implements Validator{
        @Override
    public void validate(FacesContext context, UIComponent component, Object value)
            throws ValidatorException {

        String password = (String) value;

        if(!password.contains("([1-9]/[A-Z]-[1-9]-[1-9]-[1-9](-[1-9])?")) {
            FacesMessage message = new FacesMessage();
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            message.setSummary("Value you entered is not valid - Please enter a value which contains only [1-9]/[A-Z].");
            message.setDetail("Value you entered is not valid - Please enter a value which contains only [1-9]/[A-Z].");
            context.addMessage("userForm:Password", message);
            throw new ValidatorException(message);
        }
    }
}

Anyone know why it don't accept this when I fill in admin?

String.contains does not take a regular expression; try String.matches instead.

For a regex that only matches numbers and characters, try ^\\w+$ . This basically says, "match the start of the string, then at least one 'word' character (which is a number or a letter), then match the end of the string".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM