473,569 Members | 2,703 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 10983
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:

ValidationExpre ssion="^([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
RequiredFieldVa lidator : Will make sure the control is not blank.

ReqularExpressi onValidator : 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
6395
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 the timezone. Any implementation suggestions? Thanks
6
2508
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 automatically calculate how much time is inbetween. That part of it is working fine, but what ISN'T working fine is the script that validates whether...
1
2088
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 seconds 'cause I set them to 0. I need to be able to save them in the database (Access) where the format of the field is for example: 12/22/2004...
0
1182
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 submission, but I need a validation in real time. Can someone tell me how I could simulate the equivalent of an onkeyup event with an asp:textbox?
2
2004
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 ? Is there a possibillity to use a special kind of control for that purpose,
0
1017
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
2056
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
1266
by: narend | last post by:
I want to validation in time in 24:00 Hours format.. i am using in txtbox in html
2
7112
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' <DateAdd("yyyy",-16,Date()) simple and good enough for me.
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7922
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8119
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7668
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6281
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5509
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
2111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.