|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
DATE Formathi
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 > hi See DateTime.ParseExact> 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 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 "Hans Kesting" <news.2.hansdk@spamgourmet.com> wrote in message If you want to keep it all in client-side JavaScript to avoid a postback, I 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. 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.
Other interesting topics
Pattern/ help wanted for Request
old with the new, forms... ClientScript.RegisterClientScriptBlock in ASP.NET 2.0 Passing Data Between Forms Web From, Twain and ActiveX Presentation layer depends on ???? Getting a Profile Value! how to upgrade existing ASP NET applications new to 2.0 (moving from 1.1) asp.net 2.0, atlas and sever controls |
|||||||||||||||||||||||