Hi,
Is it possible to validate multiple textboxes on a
jsp using
struts validator without having to specify each property in the validator.xml?
I have a jsp which has 20 textboxes (currQuarter1,currQuarter2,..prevQuarter1...etc)
I want to validate all of them so that the user may only enter numerical values. I am using the mask - ^[0-9]*$ and it works fine if i explicily specify the property name in the validation.xml
eg -
<form name="CheckWageCriteriaForm">
<field
property="currQtr1" depends="mask">
<msg name="mask" key="CheckWageCriteriaForm.currQtr1.maskmessage"/>
<arg key="CheckWageCriteriaForm.currQtr1"/>
<var>
<var-name>mask</var-name>
<var-value>^[0-9]*$</var-value>
</var>
</field>
</form>
However, I have 20 properties and i dont want to write the form tag for all of them since they have the same validation.
So, i wrote a custom validation class, and added it to validation-rules.xml.
WagePaymentsValidator Class :- public class WagePaymentsValidator implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
public static boolean validateAllQtrs(
Form form,
Object bean,
ValidatorAction va,
Field field,
ActionErrors errors,
HttpServletRequest request,
ServletContext application) {
//String value = ValidatorUtils.getValueAsString(
//bean,
//field.getProperty());
try
{
String currYearQtr = null;
List<String> currQtrList = new ArrayList<String>();
String prevYearQtr = null;
List<String> prevQtrList = new ArrayList<String>();
for(int i=0;i<4;i++)
{
currYearQtr = "currQtr"+i;
field = form.getField(currYearQtr);
String sProperty = field.getVarValue(currYearQtr);
String currQtrValue = ValidatorUtils.getValueAsString(bean, sProperty);
if (!GenericValidator.isBlankOrNull(currQtrValue))
{
if(!containsOnlyNumbers(currQtrValue))
return false;
}
//currQtrList.add(currQtrValue);
}
for(int i=1;i<=4;i++)
{
for(int j=1;j<=4;i++)
{
prevYearQtr = "prev"+i+"Qtr"+j;
field = form.getField(prevYearQtr);
String sProperty = field.getVarValue(prevYearQtr);
String prevQtrValue = ValidatorUtils.getValueAsString(bean, sProperty);
if (!GenericValidator.isBlankOrNull(prevQtrValue))
{
if(!containsOnlyNumbers(prevQtrValue))
return false;
}
//prevQtrList.add(prevQtrValue);
}
}
}
catch (Exception e) {
errors.add(field.getKey(),
Resources.getActionMessage(request, va, field));
return false;
}
return true;
}
/**
* This method checks if a String contains only numbers
*/
private static boolean containsOnlyNumbers(String str) {
//Check for null or empty fields
if (str == null || str.length() == 0)
return false;
for (int i = 0; i < str.length(); i++) {
//If we find a non-digit character we return false.
if (!Character.isDigit(str.charAt(i)))
return false;
}
return true;
}
}
validation-rules.xml :- <!--Custom Validator for Wage Payments page-->
<validator name="validateAllQtrs"
classname="gov.iowa.wd.ui.employer.determineliability.WagePaymentsValidator"
method="validateAllQtrs"
methodParams="org.apache.commons.validator.Form,
java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
depends=""
msg="CheckWageCriteriaForm.currQtr1.maskmessage"/>
However, the system does not validate the fields and if i enter a non-numerical value in any of the textboxes and submit, i get numberformatexception in my
ejb method as it can handle only numeric values.
Is there any other way I can achieve validation for multiple textboxes?