001// Copyright (c) 2001 Hursh Jain (http://www.mollypages.org) 002// The Molly framework is freely distributable under the terms of an 003// MIT-style license. For details, see the molly pages web site at: 004// http://www.mollypages.org/. Use, modify, have fun ! 005 006package fc.web.forms; 007 008import javax.servlet.*; 009import javax.servlet.http.*; 010import java.io.*; 011import java.util.*; 012import java.text.*; 013import java.util.regex.*; 014 015import fc.jdbc.*; 016import fc.io.*; 017import fc.util.*; 018 019/** 020Validates a date. 021 022@author hursh jain 023**/ 024public final class VDate extends FieldValidator 025{ 026static final DateFormat df; 027static final GregorianCalendar cal; 028 029static { 030 df = DateFormat.getDateInstance(DateFormat.SHORT); 031 df.setLenient(false); 032 033 cal = (GregorianCalendar) df.getCalendar(); 034 cal.setLenient(false); 035 } 036 037public VDate(AbstractText field, String errorMessage) 038 { 039 super(field, errorMessage); 040 } 041 042/** 043Works with any field that returns a <b>String</b> via it's 044{@link Field#getValue} method. The date string can be any 045valid string that is parseable by the {@link 046java.text.DateFormat.parse(String)} method (even if it's not 047semantically valid). For example, the following parses OK: 048<tt>02/<b>29</b>/1980</tt> 049 050@throws ClassCastException If the field's {@link Field#getValue} method 051 does not return a String 052**/ 053public boolean validate(FormData fd, HttpServletRequest req) 054 { 055 String val = ((AbstractText) field).getValue(fd); 056 057 if (val == null) 058 return false; 059 060 boolean result = true; 061 try { 062 df.parse(val); 063 064 } 065 catch (Exception e) { 066 result = false; 067 } 068 return result; 069 } 070 071} //~class VDate 072