You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by "Niall Pemberton (JIRA)" <ji...@apache.org> on 2006/07/19 16:43:16 UTC
[jira] Resolved: (VALIDATOR-153) [validator] add "isFuture"
validator for credit card expiration dates
[ http://issues.apache.org/jira/browse/VALIDATOR-153?page=all ]
Niall Pemberton resolved VALIDATOR-153.
---------------------------------------
Fix Version/s: 1.3.0 Release
Resolution: Fixed
Assignee: Niall Pemberton
Commons Validator has a new "routines" package which has a number of Date comparison validations:
http://jakarta.apache.org/commons/validator/apidocs/org/apache/commons/validator/routines/package-summary.html#date
To validate a credit card expiry date is a valid future month, you can now do the following:
// Get the Date validator
DateValidator validator = DateValidator.getInstance();
// Check Expiry date is valid month/year
Date expiryDate = validator.validate(fooString, "MM/yy");
if (expiryDate == null) {
// error...not a valid date
return;
}
// Check Expiry date is a future month
int compare = validator.compareMonths(expiryDate, new Date(), null);
if (compare <= 0) {
// error, not a future month
return;
}
> [validator] add "isFuture" validator for credit card expiration dates
> ---------------------------------------------------------------------
>
> Key: VALIDATOR-153
> URL: http://issues.apache.org/jira/browse/VALIDATOR-153
> Project: Commons Validator
> Issue Type: Improvement
> Components: Routines
> Environment: Operating System: other
> Platform: Other
> Reporter: Ralf Hauser
> Assigned To: Niall Pemberton
> Priority: Minor
> Fix For: 1.3.0 Release
>
>
> as a complement to creditcard:
> - no credit card will be accepted past the expiration date
> assumption: the input calendar is of the form "yyyy-mm"
> public static boolean isFuture(Object bean, ValidatorAction va,
> Field field, ActionMessages errors, HttpServletRequest request) {
> String value = ValidatorUtils.getValueAsString(bean, field
> .getProperty());
> if (!GenericValidator.isBlankOrNull(value)) {
> try {
> Date now = new Date(System.currentTimeMillis());
> Date input = null;
> try {
> //take the last of the month
> int month = Integer.parseInt(value.substring(5, 7));
> int year = Integer.parseInt(value.substring(0, 4));
> GregorianCalendar cal = new GregorianCalendar(year, month,
> 1);//astonishingly creates the last day of the
> month, but that's what we need.
> // log.debug(new
> java.util.Date(cal.getTimeInMillis()).toGMTString());
> input = new java.util.Date(cal.getTimeInMillis());
> // log.debug(input.toGMTString());
> } catch (Exception e) {
> log.debug(e.getMessage());
> e.printStackTrace();
> return false;
> }
> if (!input.after(now)) {
> errors.add(field.getKey(), Resources.getActionMessage(
> request, va, field));
> return false;
> }
> } catch (Exception e) {
> errors.add(field.getKey(), Resources.getActionMessage(request,
> va, field));
> return false;
> }
> } else {
> //return false;//if field is required
> }
> return true;
> }
> and in validator-rules.xml
> <validator name="isFuture"
> classname="org.apache.struts.validator.FieldChecks"
> method="isFuture"
> methodParams="java.lang.Object,
> org.apache.commons.validator.ValidatorAction,
> org.apache.commons.validator.Field,
> org.apache.struts.action.ActionMessages,
> javax.servlet.http.HttpServletRequest"
> depends=""
> msg="error.notFuture" />
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org