Note: Put all xml and java files into your src folder
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="default" extends="struts-default">
<action name="loginAction" class="LoginAction">
<result name="success">Welcome.jsp</result>
<result name="input">Login.jsp</result>
</action>
</package>
</struts>
validator.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator Config 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-config-1.0.dtd">
<validators>
<validator name="alphabetsOnly" class="EliteValidator" />
<validator name="digitsOnly" class="EliteValidator" />
</validators>
Login-Action.validation.xml
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
<field name="username">
<field-validator type="requiredstring">
<message>Empty Username !</message>
</field-validator>
<field-validator type="alphabetsOnly">
<message>Digits not allowed in Username</message>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<message>Empty Password !</message>
</field-validator>
</field>
<field name="mobileNumber">
<field-validator type="requiredstring">
<message>Empty Mobile Number !</message>
</field-validator>
<field-validator type="digitsOnly">
<message>Alphabets not allowed in Mobile Number</message>
</field-validator>
</field>
</validators>
LoginAction.java
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
private String username;
private String password;
private String mobileNumber;
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return the mobileNumber
*/
public String getMobileNumber() {
return mobileNumber;
}
/**
* @param mobileNumber the mobileNumber to set
*/
public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
}
EliteValidator.java
import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;
public class EliteValidator extends FieldValidatorSupport{
@Override
public void validate(Object object) throws ValidationException {
System.out.println("INSIDE VALIDATE OF ELITEVALIDATOR");
String fieldName = getFieldName();
String fieldValue = (String)getFieldValue(fieldName, object);
System.out.println("FieldName: "+fieldName);
System.out.println("fieldValue: "+fieldValue);
if(fieldName.equalsIgnoreCase("username") && fieldValue.matches(".*\\d.*")){
System.out.println("Found numbers in username");
addFieldError(fieldName, object);
} else if(fieldName.equalsIgnoreCase("mobileNumber") && (fieldValue.matches(".*\\d.*")==false)){
System.out.println("Found Alphabets in mobileNumber");
addFieldError(fieldName, object);
}
return;
}
}
Note: PUT JSP IN THE WEBCONTEXT FOLDER OF YOUR APP
Login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login</title>
<style>
.errorMessage{
color:red;
font-size: 11px;
text-align:right;
}
</style>
</head>
<body>
<center>
<s:form action="loginAction" method="post" validate="true" >
<s:textfield name="username" label="Username" />
<s:textfield name="password" label="Password" />
<s:textfield name="mobileNumber" label="Mobile Number"/>
<s:submit/>
</s:form>
</center>
</body>
</html>
Welcome.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
WELCOME TO CUSTOM VALIDATION DEMO PROJECT
</body>
</html>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="default" extends="struts-default">
<action name="loginAction" class="LoginAction">
<result name="success">Welcome.jsp</result>
<result name="input">Login.jsp</result>
</action>
</package>
</struts>
validator.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator Config 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-config-1.0.dtd">
<validators>
<validator name="alphabetsOnly" class="EliteValidator" />
<validator name="digitsOnly" class="EliteValidator" />
</validators>
Login-Action.validation.xml
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
<field name="username">
<field-validator type="requiredstring">
<message>Empty Username !</message>
</field-validator>
<field-validator type="alphabetsOnly">
<message>Digits not allowed in Username</message>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<message>Empty Password !</message>
</field-validator>
</field>
<field name="mobileNumber">
<field-validator type="requiredstring">
<message>Empty Mobile Number !</message>
</field-validator>
<field-validator type="digitsOnly">
<message>Alphabets not allowed in Mobile Number</message>
</field-validator>
</field>
</validators>
LoginAction.java
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
private String username;
private String password;
private String mobileNumber;
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return the mobileNumber
*/
public String getMobileNumber() {
return mobileNumber;
}
/**
* @param mobileNumber the mobileNumber to set
*/
public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
}
EliteValidator.java
import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;
public class EliteValidator extends FieldValidatorSupport{
@Override
public void validate(Object object) throws ValidationException {
System.out.println("INSIDE VALIDATE OF ELITEVALIDATOR");
String fieldName = getFieldName();
String fieldValue = (String)getFieldValue(fieldName, object);
System.out.println("FieldName: "+fieldName);
System.out.println("fieldValue: "+fieldValue);
if(fieldName.equalsIgnoreCase("username") && fieldValue.matches(".*\\d.*")){
System.out.println("Found numbers in username");
addFieldError(fieldName, object);
} else if(fieldName.equalsIgnoreCase("mobileNumber") && (fieldValue.matches(".*\\d.*")==false)){
System.out.println("Found Alphabets in mobileNumber");
addFieldError(fieldName, object);
}
return;
}
}
Note: PUT JSP IN THE WEBCONTEXT FOLDER OF YOUR APP
Login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login</title>
<style>
.errorMessage{
color:red;
font-size: 11px;
text-align:right;
}
</style>
</head>
<body>
<center>
<s:form action="loginAction" method="post" validate="true" >
<s:textfield name="username" label="Username" />
<s:textfield name="password" label="Password" />
<s:textfield name="mobileNumber" label="Mobile Number"/>
<s:submit/>
</s:form>
</center>
</body>
</html>
Welcome.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
WELCOME TO CUSTOM VALIDATION DEMO PROJECT
</body>
</html>