[Gambas-user] How to verify a date like dd/mm/yyyy?
Benoit Minisini
gambas at ...1...
Fri Mar 21 14:10:24 CET 2008
On vendredi 21 mars 2008, 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
Yes, but it is fast. It could be enhanced a little too.
If you nead real regular expressions, use gb.pcre. But most of the time, LIKE
is enough, and prevents you from writing horrible code full of regular
expressions. :-)
Regards,
--
Benoit Minisini
More information about the User
mailing list