473,503 Members | 9,912 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Time validation in a text box

347 Contributor
I'm trying to use required validation controls on two text boxes and tried using the following code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <asp:TextBox ID="starttimeInput" runat="server" Width="130px" Height="23px"></asp:TextBox>
  3.           <span class="style1">*</span> Start Time  <asp:RequiredFieldValidator ValidationExpression="^([1-9]|1[0-2]|0[1-9]){1}(:[0-5][0-9][aApP][mM]){1}$"
  4.             ID="StartTimeValidator" runat="server" 
  5.             ErrorMessage="You Must Supply Both a Start and an End Time" 
  6.             ControlToValidate="starttimeInput"></asp:RequiredFieldValidator>
  7.  
  8.  
but I can still enter whatever I want into the text box. What I'm trying to check for is that users are entering a time in a valid format.
Can someone please point out to me where my expression may be wrong?

Thank you



Doug
Oct 6 '10 #1
7 10969
orked
49 New Member
Hello,
you need Regular Expression validator to check on the input from user you can change validation expression on it's properties you can write this value on it
[0-2][0-3]:[0-5][0-9]
Try this i think it will help you.

Orked
Oct 6 '10 #2
dougancil
347 Contributor
Orked,

Yes sorry I was using the wrong kind of validator. My next question is in relation to this. I'm noticing that when I move from field to field that the validator checks for correct information in the text fields, is there a way that I can just have the validation run when a user presses the submit button rather than as they are typing?
Oct 6 '10 #3
orked
49 New Member
Doug,

you can make the enable state to false in the properties of validator and change this state in the code behind the button you want to click on it like this
Expand|Select|Wrap|Line Numbers
  1. protected void Button1_Click(object sender, EventArgs e)
  2.     {
  3.         RegularExpressionValidator1.Enabled = true;
  4.  
  5.     }
Orked
Oct 6 '10 #4
dougancil
347 Contributor
Ok last question on this subject,

I'm noticing that when I try to validate for time in one of my text boxes that I have the following regular expression:

ValidationExpression="^([0-1][0-9]|[2][0-3]):([0-5][0-9])$"

What that doesnt cover of course is if someone just enters in say

1:00 .. it returns an error.

What's the best way around accepting both and 1 and 2 characters for time?

Would it be something similar to date?

"^\d{1,2}\/\d{1,2}\/\d{4}$"

Thanks

Doug
Oct 6 '10 #5
Frinavale
9,735 Recognized Expert Moderator Expert
Your first regular expression will work with the addition of a "?". The ? indicates that there should be 0 or 1 repetitions of something.

So, it's pretty easy to modify your first regular expression to this so that it includes the cases where no leading 0 is provided:

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

-Frinny
Oct 7 '10 #6
dougancil
347 Contributor
Ok so I had to do some more testing on this but I have the following 3 text boxes:

Expand|Select|Wrap|Line Numbers
  1.         <asp:TextBox ID="exceptiondateInput" runat="server" Width="130px" Height="23px"></asp:TextBox>
  2.         &nbsp; <span class="style1">*</span> Exception Date &nbsp;<asp:RegularExpressionValidator 
  3.             ID="DateValidator" runat="server" 
  4.             ControlToValidate="exceptiondateInput"
  5.             ValidationExpression="^\d{1,2}\/\d{1,2}\/\d{4}$" 
  6.             ErrorMessage="Please Enter the Date Correctly"></asp:RegularExpressionValidator>
  7.         <br />
  8.         <br />
  9.         <br />
  10.         <asp:TextBox ID="starttimeInput" runat="server" Width="130px" Height="23px"></asp:TextBox>
  11.         &nbsp; <span class="style1">*</span> Start Time &nbsp;<asp:RegularExpressionValidator
  12.             ValidationExpression="^((([0]?[1-9]|1[0-2])(:|\.)[0-5][0-9]((:|\.)[0-5][0-9])?( )?(AM|am|aM|Am|PM|pm|pM|Pm))|(([0]?[0-9]|1[0-9]|2[0-3])(:|\.)[0-5][0-9]((:|\.)[0-5][0-9])?))$"
  13.             ID="StartTimeValidator" runat="server" 
  14.             ErrorMessage="You Must Supply an Start Time" 
  15.             ControlToValidate="starttimeInput"></asp:RegularExpressionValidator><br />
  16.         <br />
  17.         <br />
  18.         <asp:TextBox ID="endtimeInput" runat="server" Width="130px" Height="23px"></asp:TextBox>
  19.         &nbsp; <span class="style1">*</span> End Time &nbsp;<asp:RegularExpressionValidator 
  20.             ID="EndTimeValidator" runat="server" 
  21.             ValidationExpression="^((([0]?[1-9]|1[0-2])(:|\.)[0-5][0-9]((:|\.)[0-5][0-9])?( )?(AM|am|aM|Am|PM|pm|pM|Pm))|(([0]?[0-9]|1[0-9]|2[0-3])(:|\.)[0-5][0-9]((:|\.)[0-5][0-9])?))$"
  22.             ErrorMessage="You Must Supply an End Time" 
  23.             ControlToValidate="endtimeInput"></asp:RegularExpressionValidator>
  24.  
  25.  
and if I hit my submit button here without entering anything in those textboxes, I don't get an error. What am I missing here?
Oct 7 '10 #7
stefbek97
54 New Member
RequiredFieldValidator : Will make sure the control is not blank.

ReqularExpressionValidator : Will make sure the input is Valid.

You Need Both Per TextBox..
Oct 19 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
6381
by: Megha Vishwanath | last post by:
Hi, We have an application where my clients may be at different timezones. I need to validate time across the timezones with javascript. The date object in JavaScript does not allow me to get...
6
2498
by: Kenneth | last post by:
Hello, I'm having some serious problems debugging a script that I'm trying to make work. I'm working on a form where a user can type in a time (in the format of HH:MM), and another script...
1
2084
by: simina | last post by:
Hi... I have an "appointments" page where the user should (or not necessarily) choose a date and time for his appointment, from 6 combo boxes:year, month, day, hour, minute and AM or PM, without...
0
1177
by: Al | last post by:
Hi, I'm using an asp:textbox in a form and I want to use the equivalent of the onkeyup event of a "normal" input text. So far, it seems that all validator performs a check up only on...
2
2002
by: Chris | last post by:
Hi, I have a texbox in a webform that is supposed to hold Dates in a specific format : e.g. 20-12-2004 What is the best way to make sure that the entered data is in the correct format ? ...
0
1016
by: R.A.M. | last post by:
Hi, Do you know if using asp: validators I can have validation text in browser popup message box? Thank you very much for your help. /RAM/
1
2050
menayah
by: menayah | last post by:
Hi all, Can i use vb validators to validate a date (MM/DD/YY) from a text box ? and also time (HH:MM am/pm)? How to do this validation in a .net webform?
1
1264
by: narend | last post by:
I want to validation in time in 24:00 Hours format.. i am using in txtbox in html
2
7105
by: DrSporkeh | last post by:
Hai everyone, i need some help :o basically im trying to set some validation rules for an 'employees' table. firstly i've set it to you have to be 16 to work here under 'Date Of Birth' ...
0
7095
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7361
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4693
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3183
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
749
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
403
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.