hi all,
i am creating a custom validation for "zip" for a
jsp page.
for the above i have completed following steps ( as recommended ) but still
"zip" validation is not working .
step 1:--written a class ValidatorRules.java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.lang.Object;
import java.util.Locale;
import org.apache.commons.validator.Field;
import org.apache.commons.validator.ValidatorAction;
import org.apache.commons.validator.ValidatorUtil;
import org.apache.commons.validator.Validator;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.validator.Resources;
import org.apache.struts.action.ActionMessages;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
public class ValidatorRules{
private static final
String ZIP_REGEX = "[0-9]{5}";
public static boolean validateZip (
Object bean,
ValidatorAction va,
Field field,
ActionErrors errors,
HttpServletRequest request,
ServletContext application){
/* Create the correct mask */
Pattern mask = Pattern.compile(ZIP_REGEX);
/* Get the string value of the current field */
String zipField = ValidatorUtil.getValueAsString(bean, field.getProperty());
/* Check to see if the value is a zip code */
Matcher matcher = mask.matcher(zipField);
if (!matcher.matches()){
errors.add(field.getKey(),
Resources.getActionError(request,va,field));
return false;
}
return true;
}
}
step 2:- added statements in validation-rules.xml
<validator name="zip"
classname="ValidatorRules"
method="validateZip"
methodParams="java.lang.Object,org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest,
javax.servlet.ServletContext"
msg="userRegistration.zip/">
</validator>
step 3:- added statements in validation.xml
<field property="zip"
depends="required,zip">
<msg name="required" key="userRegistration.zip"/>
<msg name="zip" key="userRegistration.zip"/>
</field>
but despite all these, zip (custom validation) is not working.
please , anyone help me.