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
7 2506
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.
I'm using a RegularExpressionValidator.
here is sample code - protected void Page_Load(object sender, EventArgs e)
-
{
-
-
TextBox textBox1 = new TextBox();
-
textBox1.ID = "textBox1";
-
textBox1.TextMode = TextBoxMode.MultiLine;
-
textBox1.Rows = 10;
-
form1.Controls.Add(textBox1);
-
-
Button SubmitButton = new Button();
-
SubmitButton.ID = "SubmitButton";
-
SubmitButton.Text = "Submit";
-
SubmitButton.Click += new EventHandler(SubmitButton_Click);
-
form1.Controls.Add(SubmitButton);
-
-
RegularExpressionValidator datevalidator = new RegularExpressionValidator();
-
datevalidator.ControlToValidate = "textBox1";
-
datevalidator.Text = "Enter Correct Date";
-
datevalidator.ValidationExpression = @"((0[1-9]|1[012])[/]((0[1-9]|[12][0-9]|3[01])[/])?)?(19|20)\d\d";
-
form1.Controls.Add(datevalidator);
-
-
}
-
-
private void SubmitButton_Click(object sender, System.EventArgs e)
-
{
-
//Get output from textbox
-
//Value output will look like : "value1\r\nvalue2\r\nvalue3"
-
}
I'm using a RegularExpressionValidator.
here is sample code - protected void Page_Load(object sender, EventArgs e)
-
{
-
-
TextBox textBox1 = new TextBox();
-
textBox1.ID = "textBox1";
-
textBox1.TextMode = TextBoxMode.MultiLine;
-
textBox1.Rows = 10;
-
form1.Controls.Add(textBox1);
-
-
Button SubmitButton = new Button();
-
SubmitButton.ID = "SubmitButton";
-
SubmitButton.Text = "Submit";
-
SubmitButton.Click += new EventHandler(SubmitButton_Click);
-
form1.Controls.Add(SubmitButton);
-
-
RegularExpressionValidator datevalidator = new RegularExpressionValidator();
-
datevalidator.ControlToValidate = "textBox1";
-
datevalidator.Text = "Enter Correct Date";
-
datevalidator.ValidationExpression = @"((0[1-9]|1[012])[/]((0[1-9]|[12][0-9]|3[01])[/])?)?(19|20)\d\d";
-
form1.Controls.Add(datevalidator);
-
-
}
-
-
private void SubmitButton_Click(object sender, System.EventArgs e)
-
{
-
//Get output from textbox
-
//Value output will look like : "value1\r\nvalue2\r\nvalue3"
-
}
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.
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.....
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);
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+)?)+
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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....
|
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 ....
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
| |