JSF - f:validateRegex
f:validateRegex 标记用于验证字符串值是否符合所需格式。
JSF 标签
<f:validateRegex pattern = "((?=.*[a-z]).{6,})" />
标签属性
| S.No | 属性与描述 |
|---|---|
| 1 | pattern 格式化模式 |
示例应用程序
让我们创建一个测试 JSF 应用程序来测试上述标签。
| 步骤 | 描述 |
|---|---|
| 1 | 按照 JSF - 第一个应用程序 一章中的说明,在 com.tutorialspoint.test 包下创建一个名为 helloworld 的项目。 |
| 2 | 按照以下说明修改 home.xhtml。保持其余文件不变。 |
| 3 | 按照以下说明在 webapps 目录中创建 result.xhtml。 |
| 4 | 按照以下说明在包 com.tutorialspoint.test 下创建 UserData.java 作为托管 bean。 |
| 5 | 编译并运行应用程序以确保业务逻辑按要求运行。 |
| 6 | 最后,以 war 文件的形式构建应用程序并将其部署在 Apache Tomcat Web 服务器中。 |
| 7 | 按照最后一步中的说明,使用适当的 URL 启动您的 Web 应用程序。 |
UserData.java
package com.tutorialspoint.test;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name = "userData", eager = true)
@SessionScoped
public class UserData implements Serializable {
private static final long serialVersionUID = 1L;
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
home.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>JSF tutorial</title>
</h:head>
<h:body>
<h2>h:validateRegex Example</h2>
<!-- password contains lower case letters only and.
length of the password should be greater than 6. -->
<h:form>
<h:inputSecret id = "passwordInput" value = "#{userData.password}"
label = "password" >
<f:validateRegex pattern = "((? = .*[a-z]).{6,})" />
</h:inputSecret>
<h:commandButton value = "submit" action = "result"/>
<h:message for = "passwordInput" style = "color:red" />
</h:form>
</h:body>
</html>
result.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:f = "http://java.sun.com/jsf/core"
xmlns:h = "http://java.sun.com/jsf/html">
<h:head>
<title>JSF Tutorial!</title>
</h:head>
<h:body>
<h2>Result</h2>
<hr />
Password: #{userData.password}
</h:body>
</html>
完成所有更改后,让我们像在 JSF - 第一个应用程序章节中一样编译并运行应用程序。如果您的应用程序一切正常,将产生以下结果。
输入无效值。以下是输出。
输入有效值。以下是输出。

jsf_validation_tags.html