Parse a date
#1

Hello.

Upon registration, a player must enter a birth date.

But I do not know how to check if this date is real ...

It entered in DD / MM / YYYY.

Should verify that such day is not equal to it if this month is not equal to it (number of days per month) or if the year is not equal to it (leap year, it's okay to worse) ...

I can not even get the three numbers with sscanf: /

thank you
Reply
#2

pawn Код:
stock CheckDate(datestr[])
{
    new pos, slashes, numbers[3], atpos;
    while(datestr[pos] != 0)
    {
        if(datestr[pos] == ' ') continue;
        if(datestr[pos] == '/')
        {
            slashes ++;
            atpos ++;
            continue;
        }
        if(strval(datestr[pos]) < 0 || strval(datestr[pos]) > 9) return 0;
        else
        {
            numbers[atpos] ++;
        }
    }
    if(atpos > 2) return 0;
    if(numbers[0] != 2 || numbers[1] != 2 || numbers[2] != 4) return 0;
    return 1;
}
There ya go. To find out how many days per month, make a array and compare it.
Reply
#3

I don't know why people make such functions when all you need is the RegEx plugin and either find the expression online or write it yourself. Date validation is easy in regular expressions.

Код:
^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$
This will match DD/MM/YYYY OR DD.MM.YYYY OR DD-MM-YYYY OR DD.MM/YYYY

Код:
^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$
This expression is the same as above except for MM DD YYYY
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)