0

I am trying to make this AJAX ControlToolkit to accept either standard time or military time format, but any military time later than 1200 is invalidated in this version. When entering military time, I prefer 1200, 1305, etc without either A/PM or ":" separator. Can you help? Thanks.

<td class="data">
  <strong>Scheduled Time:</strong>
  <br />
  <asp:TextBox ID="scheduled_time" runat="server" Enabled="False"></asp:TextBox>
  <asp:Label ID="lblScheduled_time_tip" runat="server">
    <div style="font-size: 8pt">Tip: Type 'A' or 'P' to switch AM/PM</div>
  </asp:Label>
  <cc1:MaskedEditExtender ID="MaskedEditExtender3" runat="server" TargetControlID="scheduled_time" Mask="99:99" MessageValidatorTip="true" OnFocusCssClass="MaskedEditFocus" OnInvalidCssClass="MaskedEditError" MaskType="Time" AcceptAMPM="True" ErrorTooltipEnabled="True"
  />
  <cc1:MaskedEditValidator ID="MaskedEditValidator3" runat="server" ControlExtender="MaskedEditExtender3" ControlToValidate="scheduled_time" IsValidEmpty="False" EmptyValueMessage="Time is required" InvalidValueMessage="Time is invalid" Display="Dynamic"
  TooltipMessage="Input a time" EmptyValueBlurredText="*" ValidationExpression="^(([1-9]{1})|([0-1][1-2])|(0[1-9])|([1][0-2])):([0-5][0-9]).(([aA])|([pP]))[mM]$" InvalidValueBlurredMessage="Invalid time" ValidationGroup="MKE" />
</td>
user266909
  • 1,841
  • 3
  • 32
  • 58
  • Are you saying you want one control to accept either standard or military time formats, automatically applying the appropriate masking and validation? I'd be very surprised if that's possible. – Alan Moore Jul 06 '16 at 01:33

1 Answers1

0

^((?:0?[1-9]|1[0-2]):[0-5][0-9](?:[aA]|[pP])[mM]|[0-1][0-9][0-5][0-9]|2[0-3][0-5][0-9])$

Is a regex that will match what I think you're asking:

(?:0?[1-9]|1[0-2]):[0-5][0-9](?:[aA]|[pP])[mM] matches any valid normal time. (1-12:0-59(am/pm))

(the 0? allows for 01:23PM or 1:23PM)

or

[0-1][0-9][0-5][0-9]|2[0-3][0-5][0-9] any valid zulu (0000-2359)

the ^$ anchors ensure there is nothing else.

In case you want to see/play with what it matches: regex101

TemporalWolf
  • 7,727
  • 1
  • 30
  • 50
  • Thank you. It doesn't work. When the form is loaded the first time, the field has __:__ AM default. I enter 23:00, I got invalid time. I set the attributes to <..Mask="9999" AcceptAMPM="False" ..>, I still got invalid time. – user266909 Jul 05 '16 at 23:11
  • `23:00` is invalid... did you want it to accept mixed time like that? Currently, if you want to use a `:`, you *must* supply `am` or `pm` – TemporalWolf Jul 05 '16 at 23:23
  • with the mask 99:99, when I type 2300 it is entered as 23:00. with the mask 9999, it is entered as 2300. Either is invalid. I thin it is the mask and the AcceptAMPM throwing it off. – user266909 Jul 06 '16 at 01:49
  • @user266909 I'm not familiar with AJAX, so I'm not sure I can help with that. You can add a `:` to the zulu match if you like: – TemporalWolf Jul 06 '16 at 18:04