JSF - f: validateRegex

f: la balise validateRegex est utilisée pour valider une valeur de chaîne dans un format requis.

Balise JSF

<f:validateRegex pattern = "((?=.*[a-z]).{6,})" />

Attributs de balise

S. Non Attribut et description
1

pattern

Modèle de formatage

Exemple d'application

Créons une application JSF de test pour tester la balise ci-dessus.

Étape La description
1 Créez un projet avec un nom helloworld sous un package com.tutorialspoint.test comme expliqué dans le chapitre JSF - Première application .
2 Modifiez home.xhtml comme expliqué ci-dessous. Gardez le reste des fichiers inchangés.
3 Créez result.xhtml dans le répertoire webapps comme expliqué ci-dessous.
4 Créez UserData.java en tant que bean géré sous le package com.tutorialspoint.test comme expliqué ci-dessous.
5 Compilez et exécutez l'application pour vous assurer que la logique métier fonctionne conformément aux exigences.
6 Enfin, créez l'application sous la forme d'un fichier war et déployez-la dans Apache Tomcat Webserver.
sept Lancez votre application Web en utilisant l'URL appropriée, comme expliqué ci-dessous à la dernière étape.

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>

Une fois que vous êtes prêt avec tous les changements effectués, laissez-nous compiler et exécuter l'application comme nous l'avons fait dans le chapitre JSF - Première application. Si tout va bien avec votre application, cela produira le résultat suivant.

Entrez une valeur non valide. Voici la sortie.

Entrez une valeur valide. Voici la sortie.