• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

custom validation not working

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wont a simpler way would be to just use the available 'mask' validator (though ultimately that's what you are doing here), like so:



I havent tried the above though. If you do, let us know how it worked out.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic