Regular Expression Validator with MultiLine TextBox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wagswvu
    New Member
    • Mar 2008
    • 7

    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
  • Sick0Fant
    New Member
    • Feb 2008
    • 121

    #2
    Originally posted by wagswvu
    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.

    Comment

    • wagswvu
      New Member
      • Mar 2008
      • 7

      #3
      I'm using a RegularExpressi onValidator.

      here is sample code

      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"   
          }

      Comment

      • Sick0Fant
        New Member
        • Feb 2008
        • 121

        #4
        Originally posted by wagswvu
        I'm using a RegularExpressi onValidator.

        here is sample code

        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.

        Comment

        • wagswvu
          New Member
          • Mar 2008
          • 7

          #5
          Originally posted by Sick0Fant
          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 RegularExpressi onValidator to validate my input when the user enters multiple dates in a multi-line textbox.....

          Comment

          • Sick0Fant
            New Member
            • Feb 2008
            • 121

            #6
            Originally posted by wagswvu
            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 RegularExpressi onValidator to validate my input when the user enters multiple dates in a multi-line textbox.....
            I know nothing of the RegularExpressi onValidator.

            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(MyStringR epresentationOf MyRegularExpres sion);

            Then, too see if a string is validated:

            MyRegEx.IsMatch (StringToBeVali dated);

            Comment

            • wagswvu
              New Member
              • Mar 2008
              • 7

              #7
              Originally posted by Sick0Fant
              I know nothing of the RegularExpressi onValidator.

              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(MyStringR epresentationOf MyRegularExpres sion);

              Then, too see if a string is validated:

              MyRegEx.IsMatch (StringToBeVali dated);
              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+)?)+

              Comment

              • Sick0Fant
                New Member
                • Feb 2008
                • 121

                #8
                Originally posted by wagswvu
                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.

                Comment

                Working...