Standard Validators in JSF
The input provided by the user must be validated before processing it to the next step. Like checking the contact number, name should contain only alphabets etc.

JSF has built-in validators that is used to perform different types of DataValidation. These validators are known as Standard Validators and the tags are provided by Core tag library in JSF.

Standard Validators

 

validateDoubleRange validateLongRange validateLength validateRequired validateRegex
Used to validate Range of double inputs Used to validate Range of Long inputs Used to validate Length Similar to required attribute of UI Component Used to validate input against specified pattern

ValidatorMessage:
When we use these validators and if the user input is wrong, the system will show an error message to the user which is of system format and user wont be able to understand. To overcome this we use ValidatorMessage to give the error message in understandable format for the user.

Example:
Lets get the input from User and validate it using the above Standard Validators
Name – Should contain only Alphabets and no numbers
phone – Should have 10 numbers
Amount- Should be between 200 and 5000
Credit Card – Required Field

JSF Managed Bean – Customer.java

import javax.inject.Named;
import javax.enterprise.context.Dependent;
import javax.faces.bean.ManagedBean;

@Named(value = "customer")
@Dependent
@ManagedBean
public class Customer {
    
    String name,phone,amount,credit;

    public String getName() {
        return name;
    }

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

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }

    public String getCredit() {
        return credit;
    }

    public void setCredit(String credit) {
        this.credit = credit;
    }

    public Customer() {
    }
    
}

Input.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:c="http://java.sun.com/jsf/core">
    <h:head>
        <title>Standard Validators</title>
    </h:head>
    <h:body>
        <h:form>
            <table>
                <tr>
                    <td><h:outputLabel value="Enter the Username: "/></td>
                    <td><h:inputText value="#{customer.name}" required="true" validatorMessage="Enter only Alphabets for your name">
                            <c:validateRegex pattern="[a-zA-Z]*"/> //Regex for only Alphabets
            </h:inputText></td>
            </tr>
                <tr>
                    <td><h:outputLabel value="Enter the phone: "/></td>
                    <td><h:inputText value="#{customer.phone}" required="true" validatorMessage="10 numbers of your phone">
                <c:validateLength minimum="10" maximum="10"/> //minimum 10 numbers
                        </h:inputText></td>
                </tr>
                <tr>
                    <td><h:outputLabel value="Enter the Amount: "/></td>
                    <td><h:inputText value="#{customer.amount}" required="true" validatorMessage="Minimum is 200 and maximum is 5000 at Amount">
                <c:validateDoubleRange minimum="200" maximum="5000"/> //between 2 amounts
                        </h:inputText></td>
                </tr>
                <tr>
                    <td><h:outputLabel value="Credit Card Number: "/></td>
                    <td><h:inputText value="#{customer.credit}" validatorMessage="Enter your Credit card number">
                <c:validateRequired/> //required field
                        </h:inputText></td>
            </tr>
                <tr>
                    <td colspan="3"><center><h:commandButton value="Validate" action="Input"/></center></td>
            </tr>
            </table>
        </h:form>
    </h:body>
</html>

Output:
output1

 

 

By Sri

Leave a Reply

Your email address will not be published. Required fields are marked *