• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

Verify data in textbox

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
Richard Kreidl
Asked:
Richard Kreidl
  • 5
  • 5
  • 3
1 Solution
 
malvagiaCommented:
Use a regular expression validator.
0
 
Richard KreidlSoftware DeveloperAuthor Commented:
Not sure how a expression validator works..

Please show examples.
0
 
malvagiaCommented:
<asp:regularexpressionvalidator id="RegularExpressionValidator1" runat="server" controltovalidate="Textbox1" validationexpression="^[0-9]?[0-9]{1}:[0-9]{2}$" />
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
guru_samiCommented:
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
 
Richard KreidlSoftware DeveloperAuthor Commented:
Examples of valid data:

8:30
12:30
1:30
2:00
10:00
0
 
guru_samiCommented:
try this ^[0-9]{1,2}:[0-9]{2} as ValidationExpression for your RegularExpressionValidator control
0
 
Richard KreidlSoftware DeveloperAuthor Commented:
Not sure how to code a expression validator control????
0
 
guru_samiCommented:
To check for empty textbox you can use RequiredFieldValidator Control  in combination with RegularExpressionVC
0
 
guru_samiCommented:
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
 
Richard KreidlSoftware DeveloperAuthor Commented:
You're using an asp example. I need to use this validation code on a Windows form for a textbox.
0
 
guru_samiCommented:
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
 
malvagiaCommented:
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
 
Richard KreidlSoftware DeveloperAuthor Commented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 5
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now