我跟随老师给出的一个例子,但是当我实施它时,@ NotNull并没有与@Pattern一起使用姓名。
对于@NotNull我想在输入框旁边显示一条错误消息。出于某种原因,因为代码在这里它不会显示错误。
对于@Pattern,我试图检查名字或姓氏中是否有数字,以及是否确实返回错误。我试过看this pattern regexp constructs,但似乎无法让它正常工作。
的index.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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Sign Up</title>
</h:head>
<h:body>
<h:form id="componentForm">
<h:panelGrid columns="3">
Last name:
<h:inputText id="lastName" value="#{register.lastName}"/>
<h:messages for="lastName" errorStyle="color: red" infoStyle="color: green"/>
First name:
<h:inputText id="firstName" value="#{register.firstName}"/>
<h:messages for="firstName" errorStyle="color: red" infoStyle="color: green"/>
Password:
<h:inputSecret id="password" value="#{register.password}"/>
<h:messages for="password" errorStyle="color: red" infoStyle="color: green"/>
Email:
<h:inputText id="email" value="#{register.email}"/>
<h:messages for="email" errorStyle="color: red" infoStyle="color: green"/>
Phone number:
<h:inputText id="phoneNumber" value="#{register.phoneNumber}"/>
<h:messages for="phoneNumber" errorStyle="color: red" infoStyle="color: green"/>
Gender:
<h:selectOneRadio id="gender" value="#{register.gender}">
<f:selectItems value="#{register.genderItems}"/>
</h:selectOneRadio>
<h:messages for="gender" errorStyle="color: red" infoStyle="color: green"/>
Languages:
<h:selectManyCheckbox id="languages" value="#{register.languages}">
<f:selectItems value="#{register.languageItems}"/>
</h:selectManyCheckbox>
<h:messages for="languages" errorStyle="color: red" infoStyle="color: green"/>
Hometown:
<h:selectOneMenu id="hometown" value="#{register.hometown}">
<f:selectItems value="#{register.hometownItems}"/>
</h:selectOneMenu>
<h:messages for="hometown" errorStyle="color: red" infoStyle="color: green"/>
<h:commandButton value="Sign Up" action="#{register.show}"/>
</h:panelGrid>
</h:form>
</h:body>
</html>
register.java
package comp;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import javax.validation.constraints.Min;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.faces.model.SelectItem;
import java.util.List;
@Named
@RequestScoped
public class register {
//@NotNull(message="First name cannot be null")
@Pattern(regexp="[0-9]", message="Cannot contain numbers")
private String firstName;
@NotNull(message="Last name cannot be null")
@Pattern(regexp="[0-9]", message="Cannot contain numbers")
private String lastName;
@Min(value=4, message="Password must contain at least 4 characters")
private String password;
@NotNull(message="Must enter an email address")
@Pattern(regexp = "[a-zA-Z0-9]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]+", message = "Email format is invalid.")
private String email;
@Pattern(regexp="[0-9]*", message="Must contain only digits")
private String phoneNumber;
@NotNull(message="Must have a gender")
private String gender;
@NotNull(message="You must know one language")
private List<SelectItem> languages;
@NotNull(message="You must have hometown")
private String hometown;
public String getLastName(){
return lastName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public String getFirstName(){
return firstName;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password = password;
}
public String getEmail(){
return email;
}
public void setEmail(String email){
this.email = email;
}
public String getPhoneNumber(){
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber){
this.phoneNumber = phoneNumber;
}
public String getGender(){
return gender;
}
public void setGender(String gender){
this.gender = gender;
}
public List<SelectItem> getGenderItems(){
return Utility.genderList();
}
public List<SelectItem> getLanguages(){
return languages;
}
public void setLanguages(List<SelectItem> languages){
this.languages = languages;
}
public List<SelectItem> getLanguageItems(){
return Utility.languageList();
}
public String getHometown(){
return hometown;
}
public void setHometown(String hometown){
this.hometown = hometown;
}
public List<SelectItem> getHometownItems(){
return Utility.hometownList();
}
public String show(){
return "showinfo";
}
}
答案 0 :(得分:0)
要验证名字和姓氏,您必须匹配字母,而不是数字。
使用
@Pattern(regexp="[a-zA-Z]")
或者,匹配任何国际字母:
@Pattern(regexp="\\p{L}")