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
006 package fc.web.forms;
007
008 import javax.servlet.*;
009 import javax.servlet.http.*;
010 import java.io.*;
011 import java.util.*;
012 import java.text.*;
013 import java.util.regex.*;
014
015 import fc.jdbc.*;
016 import fc.io.*;
017 import fc.util.*;
018
019 /**
020 Validates a date.
021
022 @author hursh jain
023 **/
024 public final class VDate extends FieldValidator
025 {
026 static final DateFormat df;
027 static final GregorianCalendar cal;
028
029 static {
030 df = DateFormat.getDateInstance(DateFormat.SHORT);
031 df.setLenient(false);
032
033 cal = (GregorianCalendar) df.getCalendar();
034 cal.setLenient(false);
035 }
036
037 public VDate(AbstractText field, String errorMessage)
038 {
039 super(field, errorMessage);
040 }
041
042 /**
043 Works with any field that returns a <b>String</b> via it's
044 {@link Field#getValue} method. The date string can be any
045 valid string that is parseable by the {@link
046 java.text.DateFormat.parse(String)} method (even if it's not
047 semantically 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 **/
053 public 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