Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having a start time,end time on my web page.
I need to end time must be greater than start time and start time is current time& end time is user entry using Masked Extender
.How can I achieve this validation?
Posted

You can use:
C#
<asp:Textbox runat="server" ID="TextBox1" />
<asp:CustomValidator runat="server" ControlToValidate="TextBox1" ErrorMessage="Date was in incorrect format" OnServerValidate="CustomValidator1_ServerValidate" />

Code behind:
C#
protected void CustomValidator1_ServerValidate(object sender, ServerValidateEventArgs e)
{
    DateTime d;
    e.IsValid = DateTime.TryParseExact(e.Value, new[] { "dd/MM/yyyy", "yyyy-MM-dd" }, CultureInfo.InvariantCulture, DateTimeStyles.None, out d);
}


==OR==
C#
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:CompareValidator ID="DateValidator" runat="server" Operator="DataTypeCheck"
        Type="Date" ControlToValidate="TextBox1" ErrorMessage="Bad format" />


Good luck,
OI
 
Share this answer
 
v2
Use
<asp:comparevalidator></asp:comparevalidator>
to validate your dates
 
Share this answer
 
v2
<script type="text/javascript">

function compareTime(source, arguments) {
var start = document.getElementById("<%=txtStartTime.ClientID %>");
var end = document.getElementById("<%=txtEndTime.ClientID %>");
var starttime = new Date("November 13, 2013 " + start.value);
starttime = starttime.getTime();
var endtime = new Date("November 13, 2013 " + end.value);
endtime = endtime.getTime();
arguments.IsValid = (endtime >= starttime);
}
</script>


<asp:TextBox ID="txtStartTime" runat="server" Width="250px"/>
<asp:TextBox ID="txtEndTime" runat="server" Width="250px">


Custom validator with client side Validation Function :

VB
<asp:CustomValidator ID="cstmvEndDate" ControlToValidate="txtEndTime" ClientValidationFunction="compareTime" Display="Dynamic" ErrorMessage="End time should be greater than Start time!" ValidationGroup="Validation"runat="server" />
 
Share this answer
 
Comments
CHill60 22-May-15 8:08am    
Be careful if you are going to answer 2 year old questions that already have solutions - make it clear that you are bringing something new to the table or members may downvote your solution. In this case I would have started the solution with something like "For client side validation rather than server-side try..."

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