简体   繁体   English

JSF中的验证

[英]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 这是我的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; String.contains不使用正则表达式; try String.matches instead. 请尝试使用String.matches

For a regex that only matches numbers and characters, try ^\\w+$ . 对于仅匹配数字和字符的正则表达式,请尝试^\\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". 这基本上说:“匹配字符串的开头,然后匹配至少一个'单词'字符(是数字或字母),然后匹配字符串的末尾”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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