Click here to Skip to main content
15,885,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two textboxes with mask textbox attached for inputing time..

ASP.NET
<asp:TextBox ID="txtStartTime" runat="server" MaxLength="8" CssClass="WellInfoLables"
     Width="150"></asp:TextBox>
<asp:MaskedEditExtender ID="MaskedEditExtender_txtStartTime" runat="server"  ClearMaskOnLostFocus="False" Mask="99:99" TargetControlID="txtStartTime" MaskType="Time" UserTimeFormat="TwentyFourHour"
MessageValidatorTip="true" />

///////And ////

<asp:TextBox ID="txtEndTime" runat="server" MaxLength="8" CssClass="WellInfoLables"
    Width="150"></asp:TextBox>
<asp:MaskedEditExtender ID="MaskedEditExtender_txtEndTime" runat="server" ClearMaskOnLostFocus="False"
   Mask="99:99" TargetControlID="txtEndTime" MaskType="Time" UserTimeFormat="TwentyFourHour"
   MessageValidatorTip="true" />


now additionaly i add one comparevalidation control to my page like this...

ASP.NET
<asp:CompareValidator ID="ComValEndTime" runat="server" 
    ControlToValidate="txtEndTime" ControlToCompare="txtStartTime"
    ErrorMessage="End time is Greater then Start Time"
    Text="*" Display="Dynamic" Operator="GreaterThan" Type="Date" ValidationGroup="vgDA"></asp:CompareValidator>



but its not working..

its give error every time, event i enter the end time larger then start time, i use 24 hrs time formate..


please provide me the solution, not for this only if any better idea for validation this two time then it will also apprisiated.
Posted

go for client side validation i.e. use javascript to validate the time.split the time by 'hours','minutes' and 'seconds' then compare each. if hours are equal compare minutes,like same
wise seconds also. this will work only single day time i.e. 0.00 to 23.59 only.
 
Share this answer
 
Use this funtion in customvalidator
function timeToSec(str) 
{   
var h = Number(str.match(/^\d+/));
   if(str.indexOf("PM") != -1)    
 h += 12;   
var m = Number(str.match(/^\d+:(\d+):/)[1]); 
  var s = Number(str.match(/:(\d+)\s+[AP]M/)[1]);  
 return (h * 60 + m) * 60 + s; 
} 
if(timeToSec(start) > timeToSec(end)) 
  alert("Oops"); 
 
Share this answer
 
If the hour is 12.00 pm, h will get 24. I added the following to ensure 12.00 pm (noon) remains 12h
JavaScript
if ((time.indexOf("PM") != -1) && (h != 12))
                h += 12;
 
Share this answer
 

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