Home > ASP.Net, Javascript > Using Custom Validator Control to Compare two Dates

Using Custom Validator Control to Compare two Dates


Hi all,

try this example to Use Custom Validator Control to Compare two Dates using Client Side :-

The Javascript function :-

<script type="text/javascript">
function fncClientCheckDate(sender, args) {
if (new Date(document.getElementById("textBox1").value) > new Date(document.getElementById("textBox2").value))
{
args.IsValid = false;
return;
}
args.IsValid = true;
}
</script>

the Design should be :-

Start Date :-
<asp:TextBox ID="textBox1" runat="server"></asp:TextBox>
<cc1:CalendarExtender ID="textBox1_CalendarExtender" runat="server"
Enabled="True" TargetControlID="textBox1">
</cc1:CalendarExtender>
<br />
End Date :-
<asp:TextBox ID="textBox2" runat="server"></asp:TextBox>
<cc1:CalendarExtender ID="textBox2_CalendarExtender" runat="server"
Enabled="True" TargetControlID="textBox2" >
</cc1:CalendarExtender>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="End Date should be greater than Start Date" ClientValidationFunction="fncClientCheckDate"></asp:CustomValidator>

Hope this helps

Good Luck

Advertisements
Categories: ASP.Net, Javascript
  1. mani
    September 27, 2011 at 7:05 am

    This works…
    Thanks a lot
    Mani

  2. yasserzaid
    October 2, 2011 at 10:13 am

    @Mani :- you are welcome ..hope my post helps you

  3. WellWisher
    April 3, 2012 at 9:04 am

    Thanx a lot…

  4. yasserzaid
    April 4, 2012 at 2:57 pm

    @WellWisher :- you are welcome

  5. June 13, 2013 at 9:20 am

    thanks mani….this code helped me very much

  1. July 4, 2014 at 10:39 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: