Connecting Tech Pros Worldwide Help | Site Map

Problem with Regex statement

Newbie
 
Join Date: Oct 2009
Posts: 4
#1: Oct 1 '09
I want to validate (in the code behind, in the aspx.cs file) a date statement that goes by this format:

- Thursday, October 1rst, 2009 - 3:21 pm

I tried a few different ways, but nothing returns valid (with what I've tried), this is a basic example of what I've tried:

Edit: There was a problem that I notticed with the copy and pasting that I did when I pasted the code, so I fixed that error

var

dttxt= ((TextBox)CreateUserWizardStep1.ContentTemplateCon tainer.FindControl("DateTextBox"));


if (System.Text.RegularExpressions.Regex.IsMatch(dttx t.Text, @"^[0-9a-zA-Z-:,\s]{1,50}$")

{



}



If anyone has any suggestions, it would be greatly appreciated.
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 211
#2: Oct 1 '09

re: Problem with Regex statement


I freely admit that I'm absolutely horrible at regular expressions... which is why I cheat! I found this really helpful online tool, hopefully it helps you out :)

http://www.gskinner.com/RegExr/

Maybe that will let you debug your regular expression and figure out why it's not working as expected. A big hint: You can hover your mouse over the various parts of the expression and the tool tells you what they are supposed to do.

Good luck!
Newbie
 
Join Date: Oct 2009
Posts: 4
#3: Oct 1 '09

re: Problem with Regex statement


Thnaks for showing me to that page, but, all that showed me was that my regex is correct in theory.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,745
#4: Oct 1 '09

re: Problem with Regex statement


You could try using the DateTime.TryParse(string) method.
If your string can be cast into a date, then hurray.
If not...
Newbie
 
Join Date: Oct 2009
Posts: 4
#5: Oct 1 '09

re: Problem with Regex statement


I'll try the datetime.tryparse method.

Also, I even went to another regex test site, http://www.regular-expressions.info/, and I was able to test it against my string, and it returned as valid. So, it must be some kind of an error in the c# parsing. Has anyone else ever seen or heard of anything like this?
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,745
#6: Oct 1 '09

re: Problem with Regex statement


Quote:
if (System.Text.RegularExpressions.Regex.IsMatch(dttx t.Text, @"^[0-9a-zA-Z-:,\s]{1,50}$")
Do I interpret this RegEx right?
Does your text consist of any combination of
Letters (a-z A-Z)
Numbers (0-9)
comma (,)
Colon (:)
Hyphen (-)
Whitespace (\s)
for a length of anywhere between 1-50.

I don't see how that is going to validate your date/time string.
"I swallowed 6 goldfish" would match that criteria.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,745
#7: Oct 2 '09

re: Problem with Regex statement


Is it possible that this
Quote:
dttxt= ((TextBox)CreateUserWizardStep1.ContentTemplateCon tainer.FindControl("DateTextBox"));
Isn't finding the named control "DateTextBox" and thus later the line
t.text has nothing to work with? So you are comparing against null?
Newbie
 
Join Date: Oct 2009
Posts: 4
#8: Oct 2 '09

re: Problem with Regex statement


It is.

Ayways, thanks for all the help, but, actually I figured out another way to do what I was intending to do, it was with datetime methods instead.
Reply