Solved

Verify data in textbox

Posted on 2009-05-08
13
210 Views
Last Modified: 2012-05-06
How would I verify the data in a textbox is in the following time format:

nn:nn or n:nn
It has to contain numeric data and the colon character in the format above.
0
Comment
Question by:rkckjk
  • 5
  • 5
  • 3
13 Comments
 
LVL 1

Expert Comment

by:malvagia
ID: 24336855
Use a regular expression validator.
0
 
LVL 2

Author Comment

by:rkckjk
ID: 24336885
Not sure how a expression validator works..

Please show examples.
0
 
LVL 1

Expert Comment

by:malvagia
ID: 24336978
<asp:regularexpressionvalidator id="RegularExpressionValidator1" runat="server" controltovalidate="Textbox1" validationexpression="^[0-9]?[0-9]{1}:[0-9]{2}$" />
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 24336979
can you give exact expression you are looking for...with eg.
The expression cold look somethinglike this: ^([0-9]+):([0-9]+)
thats just a try ..not tested. so please provide example.
0
 
LVL 2

Author Comment

by:rkckjk
ID: 24336999
Examples of valid data:

8:30
12:30
1:30
2:00
10:00
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 24337052
try this ^[0-9]{1,2}:[0-9]{2} as ValidationExpression for your RegularExpressionValidator control
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Author Comment

by:rkckjk
ID: 24337068
Not sure how to code a expression validator control????
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 24337094
To check for empty textbox you can use RequiredFieldValidator Control  in combination with RegularExpressionVC
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 24337110
Here is the example:
Make sure ValidationGroup Property is set as in the e.g.
 <asp:TextBox ID="Txthr" runat="server" ValidationGroup="Txthr1"></asp:TextBox>

      <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 

          ControlToValidate="Txthr" ErrorMessage="RequiredFieldValidator" 

          ValidationGroup="Txthr1">*</asp:RequiredFieldValidator>

      <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 

          ControlToValidate="Txthr" ErrorMessage="RegularExpressionValidator" 

          ValidationExpression="^[0-9]{1,2}:[0-9]{2}" ValidationGroup="Txthr1">*</asp:RegularExpressionValidator>

      <asp:Button ID="Button2" runat="server" Text="Button" ValidationGroup="Txthr1" />

Open in new window

0
 
LVL 2

Author Comment

by:rkckjk
ID: 24337153
You're using an asp example. I need to use this validation code on a Windows form for a textbox.
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 24337223
Sorry..My bad... Not a WinForm guy...but here are my closed googled example on winForms:

http://msdn.microsoft.com/en-us/library/ms996428.aspx
http://www.codeproject.com/KB/mcpp/YuvalidatingTextBox.aspx
0
 
LVL 1

Accepted Solution

by:
malvagia earned 500 total points
ID: 24337269
Sorry, I have been programming web apps lately.
For a win form try something like this in the buttons click event:


Dim reg as new Regex

reg = @"^[0-9]?[0-9]{1}:[0-9]{1}$"

Dim valid as bool

valid = reg.IsMatch(Textbox1.Text)

Open in new window

0
 
LVL 2

Author Closing Comment

by:rkckjk
ID: 31579482
thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now