[Gambas-user] How to verify a date like dd/mm/yyyy?
Richard
richard.j.walker at ...247...
Fri Mar 21 13:57:04 CET 2008
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.
More information about the User
mailing list