DATE Format

G

Guest

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
 
H

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
 
M

Mark Rae

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top