Thursday, September 23, 2010

Server Side Numeric Validator

Few days back i discussed JavaScript function to restrict input to numeric.
Today we will see Server Side Numeric Validator for JSF you can also see AlphaNumeric Validator in JSF  and Server Side Date Validator 
Steps for creating Numeric Validator
1) Create numeric.jsp file
numeric.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%@taglib prefix="jc" uri="http://jsf-components" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%--
This file is an entry point for JavaServer Faces application.
--%>
<f:view>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>JSP Page</title>
</head>
<body>
<h:form >
<jc:AlertMessage />
<h:panelGroup>
<h:outputText id="lb_No"value="Label.:" />
<h:inputText id="tf_NO" autocomplete="off" binding="#{numeric.tf_NO}" >
<f:validator validatorId="onlyNum"/>
<f:validateLength minimum="1" maximum="6"/>
</h:inputText>
<h:commandButton id="cb_save" value="Save" action="#{numeric.bt_SAVEActionPerformed}" > </h:commandButton>
</h:panelGroup>
</h:form>
</body>
</html>
</f:view>
------------------------------------------------------------------------------------------------------------------------------
2) Create a package subin and inside subin package create two javaclass files (Numeric.java and OnlyNumericValidator.java)
Numeric.java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package subin;

import javax.faces.component.html.HtmlInputText;

/**
*
* @author SubinSuresh
*/
public class Numeric {

public void bt_SAVEActionPerformed() {
System.out.println("Debug bt_SAVEActionPerformed");
}
private HtmlInputText tf_NO =new HtmlInputText();

/**
* @return the tf_NO
*/
public HtmlInputText getTf_NO() {
return tf_NO;
}

/**
* @param tf_NO the tf_NO to set
*/
public void setTf_NO(HtmlInputText tf_NO) {
this.tf_NO = tf_NO;
}
}
--------------------------------------------------------------------------------------------------------------------------
OnlyNumericValidator.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package subin;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlInputTextarea;
import javax.faces.component.html.HtmlSelectOneMenu;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

/**
*
* @author SubinSuresh
*/
public class OnlyNumericValidator implements Validator {

public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
String compValue = null;
boolean flag = false;
if (component != null && component instanceof HtmlInputText) {
compValue = (String) ((HtmlInputText) component).getSubmittedValue();
} else if (component != null && component instanceof HtmlInputTextarea) {
compValue = (String) ((HtmlInputTextarea) component).getSubmittedValue();
}
if (compValue != null && !compValue.equalsIgnoreCase("")) {
flag = compValue.matches("[0-9]*");
if (!flag) {
if (component instanceof HtmlInputText) {
((HtmlInputText) component).setTitle("Only numbers are allowed here");
((HtmlInputText) component).setSubmittedValue("");
} else if (component instanceof HtmlInputTextarea) {
((HtmlInputTextarea) component).setTitle("Only numbers are allowed here");
} else if (component instanceof HtmlSelectOneMenu) {
((HtmlSelectOneMenu) component).setTitle("Page got some un-conditional Data");
}
throw new ValidatorException(new FacesMessage("Only numbers are allowed here"));
}
}
}
}
-------------------------------------------------------------------------------------------------------------------------
3)Mapping in faces-config.xml file
<managed-bean>
<managed-bean-name>numeric</managed-bean-name>
<managed-bean-class>subin.Numeric</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<validator>
<validator-id>onlyNum</validator-id>
<validator-class>subin.OnlyNumericValidator</validator-class>
</validator>


JSF Related topics: JCaptcha in JSF, Integrating Richfaces with JSF,Getting client and server sessionId in JSF and more.....

No comments:

Post a Comment