Home All Groups Group Topic Archive Search About
Author
3 Jul 2006 9:52 AM
close browser without Logout
hi
I have a text box in which date should be entered in dd/mm/yyyy format.
If the entered string is not a valid date in that format, it has to be
marked invalid.
How do I check that? IsDate() checks if the string matches any valid date
format, not only
one specified format. So '01/30/2006' will be valid with IsDate() which I
don't want.

Plz provide help

Author
3 Jul 2006 10:30 AM
Hans Kesting
> hi
> I have a text box in which date should be entered in dd/mm/yyyy format.
> If the entered string is not a valid date in that format, it has to be
> marked invalid.
> How do I check that? IsDate() checks if the string matches any valid date
> format, not only
> one specified format. So '01/30/2006' will be valid with IsDate() which I
> don't want.
>
> Plz provide help

See DateTime.ParseExact
You can provide the format that date-string should conform to, and it
will throw an exception (FormatException) if it's not correct.

Hans Kesting
Are all your drivers up to date? click for free checkup

Author
3 Jul 2006 11:40 AM
Mark Rae
"Hans Kesting" <news.2.hansdk@spamgourmet.com> wrote in message
news:mn.1aee7d679c7dc2f7.43821@spamgourmet.com...

> See DateTime.ParseExact
> You can provide the format that date-string should conform to, and it will
> throw an exception (FormatException) if it's not correct.

If you want to keep it all in client-side JavaScript to avoid a postback, I
use the following:

function isDate(pstrDate, pstrSplit)
{
    var astrDateParts = pstrDate.split(pstrSplit);
    if(astrDateParts[0].length != 2)    // force two-digit days
    {
        return false;
    }
    if(astrDateParts[2].length != 4)    // force four-digit years
    {
        return false;
    }
    var bytMonth;
    switch(astrDateParts[1])
    {
        case "Jan":{bytMonth = 0;break;}
        case "Feb":{bytMonth = 1;break;}
        case "Mar":{bytMonth = 2;break;}
        case "Apr":{bytMonth = 3;break;}
        case "May":{bytMonth = 4;break;}
        case "Jun":{bytMonth = 5;break;}
        case "Jul":{bytMonth = 6;break;}
        case "Aug":{bytMonth = 7;break;}
        case "Sep":{bytMonth = 8;break;}
        case "Oct":{bytMonth = 9;break;}
        case "Nov":{bytMonth = 10;break;}
        case "Dec":{bytMonth = 11;break;}
    }
    var objDate = new Date(astrDateParts[2], bytMonth, astrDateParts[0]);
    return objDate.getMonth() == bytMonth;
}

E.g. if(!isDate('01/Jan/2006','/'))...

Amend as required.

Alternatively, you could use a JavaScript regular expression - probably more
efficient... There are hundreds of examples on the net - do a Google search.

Bookmark and Share