[Gambas-user] How to verify a date like dd/mm/yyyy?

Richard richard.j.walker at ...247...
Fri Mar 21 16:43:03 CET 2008


On Friday 21 March 2008 15:11:18 Benoit Minisini wrote:
> On vendredi 21 mars 2008, Richard wrote:
> > On Friday 21 March 2008 14:44:51 Richard wrote:
> > > On Friday 21 March 2008 12:57:04 Richard wrote:
> > > > On Friday 21 March 2008 07:51:48 andy2 wrote:
> > > > > in Php i found the following script. My problem is to replicate the
> > > > > ereg function.
> > > > >
> > > > > FUNCTION ControlloData($data){
> > > > >   If(!ereg("^[0-9]{2}/[0-9]{2}/[0-9]{4}$", $data)){
> > > > >     RETURN FALSE;
> > > > >   } ELSE {
> > > > >     $arrayData = explode("/", $data);
> > > > >     $Giorno = $arrayData[0];
> > > > >     $Mese = $arrayData[1];
> > > > >     $Anno = $arrayData[2];
> > > > >     If(!checkdate($Mese, $Giorno, $Anno)){
> > > > >       RETURN FALSE;
> > > > >     } ELSE {
> > > > >       RETURN TRUE;
> > > > >     }
> > > > >   }
> > > > > }
> > > > >
> > > > > -------------------------------------------------------------------
> > > > >-- -- -- This SF.net email is sponsored by: Microsoft
> > > > > Defy all challenges. Microsoft(R) Visual Studio 2008.
> > > > > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> > > > > _______________________________________________
> > > > > Gambas-user mailing list
> > > > > Gambas-user at lists.sourceforge.net
> > > > > https://lists.sourceforge.net/lists/listinfo/gambas-user
> > > >
> > > > You could also do something similar to the PHP code:
> > > >
> > > > PUBLIC FUNCTION is_date(data AS String) AS Boolean
> > > > DIM aDateElements AS String[]
> > > > DIM iDay AS Integer
> > > > DIM iMth AS Integer
> > > > DIM iYear AS Integer
> > > >
> > > >     IF NOT (data LIKE "[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]")
> > > > THEN RETURN FALSE
> > > >     ELSE
> > > >         aDateElements = Split(data, "/")
> > > >         iDay = aDateElements[0]
> > > >         iMth = aDateElements[1]
> > > >         iYear = aDateElements[2]
> > > >         TRY RETURN IsDate(Date(iYear, iMth, iDay))
> > > >         IF ERROR THEN RETURN FALSE
> > > >     ENDIF
> > > >
> > > > END
> > > >
> > > > The Gambas LIKE operator is just as weak as the VB one and the mask I
> > > > have shown will only check for digits. It will accept digits which do
> > > > not make up a date (eg 72/33/1234); this is the same behaviour as
> > > > your PHP regular expression.
> > > >
> > > >
> > > > ---------------------------------------------------------------------
> > > >-- -- This SF.net email is sponsored by: Microsoft
> > > > Defy all challenges. Microsoft(R) Visual Studio 2008.
> > > > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> > > > _______________________________________________
> > > > Gambas-user mailing list
> > > > Gambas-user at lists.sourceforge.net
> > > > https://lists.sourceforge.net/lists/listinfo/gambas-user
> > >
> > > I suppose, in deference to the peculiar requirements of our northern
> > > trans-atlantic brethren I should have taken more care in the example to
> > > make it locale-aware.
> > >
> > > The trick, which I found when looking at the ValueBox code, is to
> > > construct a localised version of the YYYY MM DD format string for a
> > > gb.ShortDate. When we are extracting day and month from the array of
> > > date element values we must know which one is the day and which the
> > > month.
> > >
> > > Benoit did it something like this:
> > >
> > >   sDateFormat = Format(Date(3333, 11, 22), gb.ShortDate)
> > >
> > > so now the month is represented by 11 and the day by 22. All we need to
> > > do is check what the first two digits represent by looking at our date
> > > format variable before we fetch values from the array.
> > >
> > > PUBLIC FUNCTION is_date(data AS String) AS Boolean
> > > DIM aDateElements AS String[]
> > > DIM iDay AS Integer
> > > DIM iMth AS Integer
> > > DIM iYear AS Integer
> > > DIM $sDateFormat AS String
> > >
> > >     sDateFormat = Format(Date(3333, 11, 22), gb.ShortDate)
> > >
> > >     IF NOT (data LIKE "[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]")
> > > THEN RETURN FALSE
> > >     ELSE
> > >         aDateElements = Split(data, "/")
> > >         iDay =
> > > IIF(LEFT(sDateFormat,2)="22",aDateElements[0],aDateElements[1]) iMth =
> > > IIF(LEFT(sDateFormat,2)="11",aDateElements[0],aDateElements[1]) iYear =
> > > aDateElements[2]
> > >         TRY RETURN IsDate(Date(iYear, iMth, iDay))
> > >         IF ERROR THEN RETURN FALSE
> > >     ENDIF
> > >
> > > END
> > >
> > > -----------------------------------------------------------------------
> > >-- This SF.net email is sponsored by: Microsoft
> > > Defy all challenges. Microsoft(R) Visual Studio 2008.
> > > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> > > _______________________________________________
> > > Gambas-user mailing list
> > > Gambas-user at lists.sourceforge.net
> > > https://lists.sourceforge.net/lists/listinfo/gambas-user
> >
> > One more attempt to get it right...
> >
> > I overlooked the assumption about the date format in the LIKE expression
> > and the possibility to tighten up the comparison mask to make it a little
> > more robust (not much).
> >
> > PUBLIC FUNCTION is_date(data AS String) AS Boolean
> > DIM aDateElements AS String[]
> > DIM iDay AS Integer
> > DIM iMth AS Integer
> > DIM iYear AS Integer
> > DIM sDateFormat AS String
> > DIM sMask AS String
> >
> >     sDateFormat = Format(Date(3333, 11, 22), gb.ShortDate)
> >
> >     IF LEFT(sDateFormat,2)="22" THEN
> > 	sMask = "[0-3][0-9]/[0-1][0-9]/[0-9][0-9][0-9][0-9]"
> >     ELSE
> > 	sMask = "[0-1][0-9]/[0-3][0-9]/[0-9][0-9][0-9][0-9]"
> >     ENDIF
> >
> >     IF NOT (data LIKE sMask) THEN
> >         RETURN FALSE
> >     ELSE
> >         aDateElements = Split(data, "/")
> >         iDay =
> > IIF(LEFT(sDateFormat,2)="22",aDateElements[0],aDateElements[1]) iMth
> > =IIF(LEFT(sDateFormat,2)="11",aDateElements[0],aDateElements[1]) iYear =
> > aDateElements[2]
> >         TRY RETURN IsDate(Date(iYear, iMth, iDay))
> >         IF ERROR THEN RETURN FALSE
> >     ENDIF
> >
> > END
> >
> >
> > Richard
>
> RETURN IsDate(Val(data)) is not complex enough? :-)

You got me there! That gets rid of the possibility of the ERROR on the Date 
conversion, thus removing the need for the TRY.

Presumably it also takes care of localisation so the splitting of the array 
into date elements is redundant, as is the LIKE check against a mask string.

My only excuse is that I wanted to make it "look" functionally similar to the 
PHP version, and show off some of Gambas's neat string related features (like 
Split).


Richard
(properly chastened, but not cowed :-)




More information about the User mailing list