Table of contents
1.
Introduction
2.
JSF
3.
validateRegex Tag
4.
Attributes
5.
Sample Program
6.
Frequently Asked Questions
6.1.
What is JSF?
6.2.
What is a regular expression?
6.3.
What is a package?
6.4.
What is a tag?
6.5.
What are import statements?
7.
Conclusion
Last Updated: Mar 27, 2024

JSF validateRegex

Author Manish Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Have you ever wondered how password validation happens when you create it for the first time? In the background, pattern matching lets you know whether you have made a password that matches the requirements. Regular expressions help to implement this idea of pattern matching. This blog will teach you how to validate regex (regular expression) in JSF using the validateRegex tag.

JSF

JSF(Java Server Faces) is a server-side framework for developing user interfaces. It is written in Java, follows a component-based architectural style, and provides standard tools for managing these components. It contains powerful APIs and a large set of tag libraries.

The APIs provide components and manages their state. It facilitates server-side validation, extensibility, accessibility and data conversions. Tag libraries help set up elements on web pages and connect them with objects on the backend. It contains handlers that implement component tags.

validateRegex Tag

f:validateRegex is a new validator tag introduced in JSF version 2.0. It helps to validate a JSF component with a given regular expression pattern.

Example:

<h:inputSecret id="password" value="#{ninja.password}">
  <f:validateRegex pattern="^([a-z]+(.)?[\s]*)$" />
</h:inputSecret>

Here passwords can only be in the lowercase alphabet.

Syntax: <f:validateRegex pattern="^([a-zA-Z]+(.)?[\s]*)$" />  

The structure of this tag is similar to any HTML tag with a '<' opening angle bracket, then comes the tag name and attributes and then the final closing angle bracket. It is a self-closing tag.

f: attribute – It passes attribute values to a component or parameter via a listener.

Attributes

validateRegex has a variety of attributes:

pattern:-  It is a mandatory attribute to define the required design.

binding:- A value expression that evaluates to an instance of RegexValidator.

disabled:- A boolean attribute to determine whether this tag is enabled at the page level or not.

Sample Program

This program will test the password entered by the user. The regex pattern requires a 6 to 20 characters string with at least one digit, one lower case letter, one upper case letter and one special symbol ("#@$%"). Validation will result in a strong and complex password.

User-managed bean code

package com.CodingNinjas;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name="ninja")
@SessionScoped
public class UserBean implements Serializable{
String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

 

JSF Page

The JSF XHTML page helps to show the use of the validateRegex tag.

< ?xml version="1.0" encoding="UTF-8"? >
<html xmlns="https://www.w3.org/1999/xhtml"   
     xmlns:f="https://java.sun.com/jsf/core"
     >
   <h:body>
    
   <h1>JSF 2 validateRegex example</h1>
   <h:form>
     <h:panelGrid columns="3">
Enter your password : 
<h:inputSecret id="password" value="#{ninja.password}" 
size="20" required="true"
label="Password">
<f:validateRegex 
                 pattern="((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})" />
</h:inputSecret>
<h:message for="password" style="color:red" />
     </h:panelGrid>
     <h:commandButton value="Submit" action="result" />
   </h:form> 
   </h:body>
</html>

Output:

If there is a pattern mismatch error message is displayed.

Frequently Asked Questions

What is JSF?

JSF is a UI development framework similar to React. It is based on components as its building blocks. It connects parts to data sources, directly improving security and efficiency.

What is a regular expression?

It is a sequence of characters that specifies the search pattern in a text. It is used for ‘find’ or ‘find and replace operations on strings or to validate input.

What is a package?

A package is a namespace similar to a folder where you can put your source files. It organises related classes and interfaces. You might keep java code in one folder, images in other and so on.

What is a tag?

A tag is a syntactical way of implementing program logic. These tags are like keywords which define how to display elements on the web browser. For example <html>, <img> etc.

What are import statements?

Import statement fetches pre-written libraries, which help in the faster development of applications. These libraries are provided by organisations or language developers.

Conclusion

Finally, you have made it to the end of this article. Congratulations!! In this blog, you learnt about regex validation in JSF. You went through the tag, its attributes and a sample program.

After reading these interview questions, are you not feeling excited to read more articles on the topic of JSF? Don't worry; Coding Ninjas has you covered. To learn, see the JSF.

Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enrol in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.

Please do upvote our blogs if you find them helpful and informative!

Happy learning!

Live masterclass