Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,


How to validate the date within a string..?? I want the server-side validation.I want the regular expression to validate date within a string or statement...please suggest server side code for this question.

For ex:"DOB:02-02-1987" or "DOB:14th may 1987"



Thanks
Posted
Updated 29-Mar-12 22:43pm
v3
Comments
Sergey Alexandrovich Kryukov 19-Mar-12 0:07am    
"I want..." is not a question. The problem is: you described just two cases, one of them is not standard ("14th"). Are you sure you need it? You need to describe all cases, then the problem is simple enough. Do you want client-side or server-side validation?
--SA

Please see my question. I hope you can decide to work without non-standard things like "14th".

You don't really need to use Regular Expressions. It will be way more reliable method to parse string as a date or time structure. In case of success, it's valid; if parsing fails, it is not. Very simple.

On client side, it will be JavaScript like this:

JavaScript
function IsValidDate(dateString) {
    return !isNaN(Date.parse(dateString));
}


On server side, you need to do the same thing using System.DateTime methods Parse or ParseExact. Unlike JavaScript, you will need to supply input string in exact format corresponding to required culture information (IFormatProvider) and/or format specifiers; but for Parse method, there are variants: date only, time only, both, and the culture should correspond current thread culture.

Besides, you need to call such method in the try-catch block as it can catch exception. For example:

C#
static bool IsValidDate(string value) {
   try {
        System.DateTime.Parse(value);
        return true;
   } catch(System.FormatException) {
        return false;
   } //exception
} //IsValidDate


Please see:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^].

For time string formats, please see:
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^].

—SA
 
Share this answer
 
Client Side Date Validation for Date Format of type: "02-02-1987"

When using text field date, Double type (decimal places or different decimal characters in globalization etc) and currency validation on client-side we often use regular expression validator and try to write complex regular expressions. The easiest way to validate these data types is to use compare validator. I think it’s the most powerful and awesome control for validating data types. All you have to do is to set

Operator=”DataTypeCheck”
And

Type=”<<your required type here >>>”
It supports following types:

Date
Currency
Double
Integer
String
Look at the following examples:

This validates the date according to current culture of the application.
ASP.NET
<asp:textbox id=""txtDateStart"" cssclass=""input"" runat=""server"" width=""200″" xmlns:asp="#unknown">

ASP.NET
<asp:comparevalidator id=""cmprValidatorDateStart"" controltovalidate=""txtDateStart"" xmlns:asp="#unknown">
    Type="Date" Display="Dynamic" Operator="DataTypeCheck" 
    ErrorMessage="*Not a valid date." runat="server"<>/asp:CompareValidator>
 
Share this answer
 
v4
In code behind use the DateTime.TryParseExact function.
 
Share this answer
 
HTML
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"(?<info>.+)\s*:\s*(?<date>\d+[-/.]\d+[-/.]\d+)");</date></info>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900