JSF Validate Length Input Field Example

This tutorial shows how to use f:validateLenght JSF Validate Length Input Field validate. This validator validates if an input field has the specified length. This element takes two attributes minimum and maximum to configure the required length.

JSF Managed Bean

package com.memorynotfound.jsf;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

public class UserBean {

    private String name;

    public String getName() {
        return name;

    public void setName(String name) {
        this.name = name;

Validate Emtpy Input

If you are experiencing that JSF does not validate empty fields, then you should add the following snippet to your web.xml. Using this property tells JSF to validate empty fields.


JSF Validate Length Input Field Page

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml"
    <h2>JSF validate length input field example</h2>
        <h:panelGrid columns="2">
            <h:outputLabel value="Name:">
                <h:inputText label="Name" id="name" value="#{userBean.name}">
                    <f:validateLength minimum="2" maximum="6"/>
            <h:message for="name" style="color:red" />
        <h:commandButton value="submit" action="result"/>


URL: http://localhost:8080/jsf-validate-length/

JSF Validate Length – Minimum

jsf validate length minimum

JSF Validate Length – Maximum

jsf validate length maximum



You may also like...