Saturday, September 18, 2010

AlphaNumeric Validator in JSF

Few days back i discussed Javascript function to restrict special characters,
Today we will see serverside alphanumeric validator for Jsf.
This serverside validator is used to validate and restrict special character entering into database.

Steps for creating AlphaNumeric Validator
1) Create AlphaNumeric.jsp file
AlphaNumeric.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="#{alphanumeric.tf_NO}" >
<f:validateLength maximum="6" minimum="1"/>
<f:validator validatorId="alphaNumeric"/>
</h:inputText>
<h:commandButton id="cb_save" value="Save" action="#{alphanumeric.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 (AlphaNumeric.java and AlphaNumericValidator.java)
AlphaNumeric.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 AlphaNumeric {

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;
}
}
--------------------------------------------------------------------------------------------------------------------------
AlphaNumericValidator.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 subin
*/
public class AlphaNumericValidator 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("[a-zA-Z0-9]*");
}
if (!flag) {
if (component instanceof HtmlInputText) {
((HtmlInputText) component).setTitle("No special symbols are allowed here");
} else if (component instanceof HtmlInputTextarea) {
((HtmlInputTextarea) component).setTitle("No special symbols are allowed here");
} else if (component instanceof HtmlSelectOneMenu) {
((HtmlSelectOneMenu) component).setTitle("Page got some un-conditional Data");
}
throw new ValidatorException(new FacesMessage("No special symbols are allowed"));
}
}
}
-------------------------------------------------------------------------------------------------------------------------
3)Mapping in faces-config.xml file

<managed-bean>
<managed-bean-name>alphanumeric</managed-bean-name>
<managed-bean-class>subin.AlphaNumeric</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<validator>
<validator-id>alphaNumeric</validator-id>
<validator-class>subin.AlphaNumericValidator</validator-class>
</validator>

When we enter some specialcharacter in textbox and click on save button it shows a popup alert No special symbols are allowed.
Note:This validation occur before entering action method of save button i.e why "Debug bt_SAVEActionPerformed" is not printed in the output console.


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

2 comments:

  1. AlphaNumericValidator was really helpfull.
    can you post serverside date validator

    ReplyDelete
  2. ServerSide date validator
    http://subinsuresh.blogspot.com/2010/09/serverside-date-validator.html

    ReplyDelete