Date Validation and MatchText

Alex Tweedly alex at tweedly.net
Thu Dec 22 15:04:02 EST 2005


Jeff Honken wrote:

>Thanks for the reply Ken that does work well.  I'd like to try to
>restrict the date to a pattern of mm/dd/yyyy.  Using "date" allow dates
>such as mm/d/y.  Do you know how to parse the date string to match
>mm/dd/yyyy only?
>
>  
>
The matchText string in your earlier email would ensure that it is in 
the right format and approximately a correct date (e.g. it would have 
allowed 02/31/1971). That in conjunction with "is a date" should give 
you everything you want ....

function CheckDate pDateToCheck
  if pDateToCheck is a date and \
matchText(pDateToCheck,"(0[1-9]|1[0-2])/(0[1-9]|[12][0-9]|3[01])/([0-9][
0-9][0-9][0-9])") \
  then
    return pDateToCheck
  else
    return "Bad date"
  end if
end CheckDate


You originally said "...  but I'm having a hard time figuring out how 
it's working." Here's a quick explanation

matchText checks a string against a regular expression. The reg expression

"(0[1-9]|1[0-2])/(0[1-9]|[12][0-9]|3[01])/([0-9][0-9][0-9][0-9])"
can be described as

0 followed by any digit between 1 and 9 (i.e. 01, 02, 03, 04, 05, 06, 07, 08, 09)
OR
1 followed by digit between 0 and 2 (i.e. 10, 11, 12)

(that's the  month taken care of)

followed by a "/"

followed by 
0 followed by any digit between 1 and 9 (i.e. 01, 02, 03, 04, 05, 06, 07, 08, 09)
OR
1 or 2 followed by digit between 0 and 9 (i.e. 10 .... 29)
OR 
3 followed by 0 or 1 or 2

(the day of the month)


followed by a "/"

followed by 4 digits

(that's the year done)

In this case, provided you're doing the "is a date" check which will 
catch invalid numbers for month and day, you could simplify the reg expr to
"\d{2}/\d{2}/\d{4}"

\d means any digit
{2} means "the previous group must occur 2 times"


-- 
Alex Tweedly       http://www.tweedly.net



-- 
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.371 / Virus Database: 267.14.3/209 - Release Date: 21/12/2005




More information about the use-livecode mailing list