473,386 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Regular Expression Validator with MultiLine TextBox

I have created a regular expression to validate dates in MM/DD/YYYY, MM/YYYY, and YYYY. "((0[1-9]|1[012])[/]((0[1-9]|[12][0-9]|3[01])[/])?)?(19|20)\d\d" . How can I make this expression work with a multi-line textbox, so that a user would be able to enter multiple dates (each on a new line) and still be validated by my expression.

Example: 3 dates entered into textbox, all be validated by expression.
Multi-Line TextBox:
01/01/2008
01/2007
2006
Jun 3 '08 #1
7 2506
Sick0Fant
121 100+
I have created a regular expression to validate dates in MM/DD/YYYY, MM/YYYY, and YYYY. "((0[1-9]|1[012])[/]((0[1-9]|[12][0-9]|3[01])[/])?)?(19|20)\d\d" . How can I make this expression work with a multi-line textbox, so that a user would be able to enter multiple dates (each on a new line) and still be validated by my expression.

Example: 3 dates entered into textbox, all be validated by expression.
Multi-Line TextBox:
01/01/2008
01/2007
2006
Well, you can split the input by newline, then use a for each loop to validate each one. Although, I'd wonder if you shouldn't just use a date-time picker to populate a listbox-- then you wouldn't need to validate it yourself because the date-time picker returns a date data type.
Jun 3 '08 #2
I'm using a RegularExpressionValidator.

here is sample code

Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e)   
  2.     {   
  3.  
  4.         TextBox textBox1 = new TextBox();   
  5.         textBox1.ID = "textBox1";
  6.         textBox1.TextMode = TextBoxMode.MultiLine;
  7.         textBox1.Rows = 10; 
  8.         form1.Controls.Add(textBox1);       
  9.  
  10.         Button SubmitButton = new Button();   
  11.         SubmitButton.ID = "SubmitButton";   
  12.         SubmitButton.Text = "Submit";   
  13.         SubmitButton.Click += new EventHandler(SubmitButton_Click);   
  14.         form1.Controls.Add(SubmitButton);   
  15.  
  16.         RegularExpressionValidator datevalidator = new RegularExpressionValidator();   
  17.         datevalidator.ControlToValidate = "textBox1";   
  18.         datevalidator.Text = "Enter Correct Date";   
  19.         datevalidator.ValidationExpression = @"((0[1-9]|1[012])[/]((0[1-9]|[12][0-9]|3[01])[/])?)?(19|20)\d\d";    
  20.         form1.Controls.Add(datevalidator);   
  21.  
  22.     }   
  23.  
  24.     private void SubmitButton_Click(object sender, System.EventArgs e)   
  25.     {   
  26.         //Get output from textbox   
  27.         //Value output will look like : "value1\r\nvalue2\r\nvalue3"   
  28.     }  
Jun 3 '08 #3
Sick0Fant
121 100+
I'm using a RegularExpressionValidator.

here is sample code

Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e)   
  2.     {   
  3.  
  4.         TextBox textBox1 = new TextBox();   
  5.         textBox1.ID = "textBox1";
  6.         textBox1.TextMode = TextBoxMode.MultiLine;
  7.         textBox1.Rows = 10; 
  8.         form1.Controls.Add(textBox1);       
  9.  
  10.         Button SubmitButton = new Button();   
  11.         SubmitButton.ID = "SubmitButton";   
  12.         SubmitButton.Text = "Submit";   
  13.         SubmitButton.Click += new EventHandler(SubmitButton_Click);   
  14.         form1.Controls.Add(SubmitButton);   
  15.  
  16.         RegularExpressionValidator datevalidator = new RegularExpressionValidator();   
  17.         datevalidator.ControlToValidate = "textBox1";   
  18.         datevalidator.Text = "Enter Correct Date";   
  19.         datevalidator.ValidationExpression = @"((0[1-9]|1[012])[/]((0[1-9]|[12][0-9]|3[01])[/])?)?(19|20)\d\d";    
  20.         form1.Controls.Add(datevalidator);   
  21.  
  22.     }   
  23.  
  24.     private void SubmitButton_Click(object sender, System.EventArgs e)   
  25.     {   
  26.         //Get output from textbox   
  27.         //Value output will look like : "value1\r\nvalue2\r\nvalue3"   
  28.     }  
Is this for an assignment? If so, do it to your specs. I'm just saying that you never want to reinvent the wheel. The date-time picker gives you a valid date, so you wouldn't have to validate it yourself. If the idea is that the user will be entering so many dates that the dtp would be cumbersome, you may want to look ate date.TryParse().

BTW, why create the controls at run time? Especially if you're doing this in the load event, you might as well optimize performance and create the controls at design time.

EDIT:

I forgot that what is being inputed might not be only the year. I'm not sure that TryParse would be successful with such an input. So, like I said before, split the text of the text box by the newline character, trim any spaces, then use a foreach to validate each line using your regex.
Jun 3 '08 #4
Is this for an assignment? If so, do it to your specs. I'm just saying that you never want to reinvent the wheel. The date-time picker gives you a valid date, so you wouldn't have to validate it yourself.

BTW, why create the controls at run time? Especially if you're doing this in the load event, you might as well optimize performance and create the controls at design time.
No, I'm working a proof of concept for a business project. The reason I can't use the datetime picker is because the data that will be inputed in either MM/DD/YY, MM/YYYY, or YYYY.(this is document metadata, so it's need to be inputed as is) As for the code it's just a crappy concept I made to mess with the expression. What I am trying to figure out is how to get the RegularExpressionValidator to validate my input when the user enters multiple dates in a multi-line textbox.....
Jun 3 '08 #5
Sick0Fant
121 100+
No, I'm working a proof of concept for a business project. The reason I can't use the datetime picker is because the data that will be inputed in either MM/DD/YY, MM/YYYY, or YYYY.(this is document metadata, so it's need to be inputed as is) As for the code it's just a crappy concept I made to mess with the expression. What I am trying to figure out is how to get the RegularExpressionValidator to validate my input when the user enters multiple dates in a multi-line textbox.....
I know nothing of the RegularExpressionValidator.

I've only used the .NET RegEx object. To validate if a string is accepted by your regular expression, first declare a RegEx object:

RegEx MyRegEx = new RegEx(MyStringRepresentationOfMyRegularExpression) ;

Then, too see if a string is validated:

MyRegEx.IsMatch(StringToBeValidated);
Jun 3 '08 #6
I know nothing of the RegularExpressionValidator.

I've only used the .NET RegEx object. To validate if a string is accepted by your regular expression, first declare a RegEx object:

RegEx MyRegEx = new RegEx(MyStringRepresentationOfMyRegularExpression) ;

Then, too see if a string is validated:

MyRegEx.IsMatch(StringToBeValidated);
somebody help me on another forum.. FYI, here is the answer:

(((0[1-9]|1[012])[/]((0[1-9]|[12][0-9]|3[01])[/])?)?(19|20)\d\d(\s+)?)+
Jun 4 '08 #7
Sick0Fant
121 100+
somebody help me on another forum.. FYI, here is the answer:

(((0[1-9]|1[012])[/]((0[1-9]|[12][0-9]|3[01])[/])?)?(19|20)\d\d(\s+)?)+
Next time, say that you need help with the regular expression itself. I assumed that you had a valid re and merely wanted to know how to validate a string with it. The unix forum is a better place for this question.
Jun 4 '08 #8

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

Similar topics

3
by: Mark | last post by:
To validate the length of a multiline textbox, I'm told that I have to use a regular expression validator. The regular expression below limits it to 25 characters in length, but if the user enters...
2
by: VSK | last post by:
Hi all, I have a .ascx file with dropdownbox (SSN, EmpName) textbox submit button regular expression validator( controltovalidate is the above textbox) Now i want to change the Regular...
2
by: S.Kartikeyan | last post by:
I have the following problem. I am using the follwing Regular Expression validator(REV) with validator expressions ^{1,2}$ ^{3,20}$ The idea of the first exp is 1 or 2 digits the idea of second...
2
by: Dot net work | last post by:
Hello. Say I have a .net textbox that uses a .net regularexpressionvalidator. If the regular expression fails, is it possible to launch a small client side javascript function to do something,...
7
by: Chris Kennedy | last post by:
Does anyone know a regular expression that will validate the file extension but also allow multiple file extensions if necessary. It also needs to be case insensitive. Basically, what I want is to...
5
by: Kerry | last post by:
Please help. I need a regular expression that parses a stream of up to 450 characters into 15 separate strings of up to 30 characters each. The regex must break at newlines. Ideally, the regex...
6
by: David | last post by:
I'm having trouble getting the regular expression validator to work with a text box. In this simple example I only want lower case letters to be allowed. So I tried the following and it doesn't...
2
by: Joey | last post by:
Hello guys, I'm trying to learn about regular expressions. I need to be able to use an RE that can evaluate for STRINGS (or specific sequences of characters), not just occurances of characters....
0
by: durga2005 | last post by:
hi i just added one textbox and regular expression validator in the form and set the properties for regular expression validator control. then i placed these two controls inside update panel ....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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,...
0
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...

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.