Ram Singh Ram Singh - 1 month ago 15
Javascript Question

how to validate 24 hour formated Time in Jquery or javascript?

I want to validate 24 hour formatted Time, Time is in the following format.

HH:MM:SS


How could i go for it. Please help me.
My HTMl Code is

<asp:TextBox Width="120px" MaxLength="20" ID="txtEndTime" runat="server"></asp:TextBox>
<ajaxctrl:maskededitextender id="metxtEndTime" runat="server" targetcontrolid="txtEndTime"
mask="99:99:99" messagevalidatortip="true" masktype="Number" inputdirection="LeftToRight"
clearmaskonlostfocus="false" acceptnegative="None" errortooltipenabled="True" />

Answer

To only validate the format, you can use this:

var valid = (timeStr.search(/^\d{2}:\d{2}:\d{2}$/) != -1);

If you're trying to validate the values as well, you can try this:

var valid = (timeStr.search(/^\d{2}:\d{2}:\d{2}$/) != -1) &&
            (timeStr.substr(0,2) >= 0 && timeStr.substr(0,2) <= 24) &&
            (timeStr.substr(3,2) >= 0 && timeStr.substr(3,2) <= 59) &&
            (timeStr.substr(6,2) >= 0 && timeStr.substr(6,2) <= 59);