You can use the asp:CustomValidator control of ASP.NET to achieve the mentioned functionality.
The HTML would look like:
<input type="text" id="TextBox1" runat="server" name="TextBox1">
<asp:CustomValidator Runat="server" ControlToValidate="TextBox1" EnableClientScript="False" ErrorMessage="Invalid DateTime Format"
ID="Customvalidator1" ClientValidationFunction="ValidateTextBox1" OnServerValidate="CustomValidaorMethod"></asp:CustomValidator>
and on server side (code behind of the page) you would have a public method like :
public void CustomValidaorMethod(object source, ServerValidateEventArgs args)
{
string valuePassed = string.Empty;
if(args.Value != null)
valuePassed = args.Value;
DateTime dt = DateTime.MinValue;
args.IsValid = true;
try
{
IFormatProvider format = new CultureInfo( "en-US");
//write custom code to validate date time or just parse it
using DateTime.Parse
dt = DateTime.Parse(valuePassed,format,DateTimeStyles.None);
}
catch
{
args.IsValid = false;
}
}
This would validate the Textbox content, also when you want to compare, you can use asp:CompareValidator to do it:
it would look like
<asp:CompareValidator ControlToCompare="TextBox2" ControlToValidate="TextBox1" EnableClientScript="true" Runat="server" ID="CompareValidator" Type='Date' Operator="Equal" ErrorMessage="Dates do not match" Enabled="True"></asp:CompareValidator>
One thing that must be kept in mind when using Asp.Net validators that post back methods should always check for Page.IsValid value that tells the page if validators returned true or false and then proceed further.
There is one more exciting thing you can do: you can enable client script function and write a client script function to do it using a script language say javascript.
(This i leave for you to figure out)
Thanks,
Ashwani