Validate Time field

Hi Experts,

I am using the following CompareValidator to validate my Date field          

 <asp:CompareValidator ID="cvDate" runat="server" ControlToValidate="txtDate" Display="Dynamic" ErrorMessage="Please enter valid Date." Operator="DataTypeCheck" SetFocusOnError="True" Type="Date"></asp:CompareValidator>

.... how do I validate a Time field so that the value is similar to:     11:47 AM ?    
simonm_jpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ramesh SrinivasTechnical ConsultantCommented:
I takeit Type="Time" does not work?
ayha1999Commented:
Hi,

try this regularexpression validator.

<asp:regularexpressionvalidator id="valtime" runat="server" ErrorMessage="A valid time required." ControlToValidate="YourField" ValidationExpression="(?<Time>^(?:0?[1-9]:[0-5]|1(?=[012])\d:[0-5])\d(?:[ap]m)?)">*</asp:regularexpressionvalidator>

validates 12:00am,01:00pm formats.

hope this helps.

ayha
simonm_jpAuthor Commented:
Saleek ... Time is not a type of CompareValidator (in Whidbey) ....

ayha1999 ... thanks for the example, but I am getting "Syntax error in regular expression" when I run it ...
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

ayha1999Commented:
Hi,

this should work.

<asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server" ErrorMessage="Your message" ControlToValidate="TextBox1"
ValidationExpression="^([1-9]|1[0-2]|0[1-9]){1}(:[0-5][0-9][aApP][mM]){1}$"></asp:RegularExpressionValidator>

hope this works.

ayha

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ayha1999Commented:
hi,

acceptable formats are


08:00AM, 10:00am, 7:00pm etc.

ayha
simonm_jpAuthor Commented:
Perfect. Thanks ayha1999.  :)
simonm_jpAuthor Commented:
Sorry ayha ... it seems it will not allow strings in the ToShortTime() format, such as:

10:32 AM (with the space between '0' and 'AM') it will only accept 10:32AM ... how will I change it so it allows ShortTime format?

Cheers,
ayha1999Commented:
HI,

you have to use 10:32AM  instead of 10:32 AM. Just play with the regexp and try.

ayha
firstmaloneCommented:
I use the following RegularExpressionValidator in ASP.NET 2.0 with a VB.NET back end.
the validator accepts h:mm or hh:mm plus one or more spaces plus am or pm or a.m. or p.m. or AM or PM or A.M. or P.M.
The code in the background strips the periods from the text before parsing it to a time
There is redundency in the code, but I'd rather ensure the data i receive even if it was submitted by-passing the form itself is valid.
I use an bollean to determin if I should continue processes or not which is the _ExceptionFound variable.


ASP.NET TextBox Control and RegularExpressionValidator Control
<asp:TextBox ID="txtEndTime" runat="server" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Time Is Not Valid. hh:mm AM/PM" ControlToValidate="txtEndTime" ValidationExpression="^(1[0-2]|0[1-9]|[1-9]):[0-5][0-9]\s+([ap]\.?m\.?|[AP]\.?M\.?)" />
CODE
 _PostTimeEnd = txtEndTime.Text.Replace(".", "").ToUpper
If DateTime.TryParse(_PostTimeEnd, _TimeEnd) Then
     _PostTimeEnd = _TimeEnd.ToShortTimeString
Else
     _ExceptionFound = True
           Me.lblError.Text = "Hearing End Time Is Not Valid. hh:mm AM/PM"
End If

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.