.NETGURU
Date + Time Validator
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngregexp' list.


Melissa Whalen
Hi,

I'm trying to get a regex that will validate a date and time. The date has
to be there, but the time can be an optional entry. I've looked through
regexlib.com, but I can't seem to find exactly what I'm looking for. I've
tried combining a couple to get what I need, but I'm not sure of all the
syntax so I wasn't able to get it to work.
Here's what I've tried (taken from regexlib.com):
^((((0[13578])|([13578])|(1[02]))[\/](([1-9])|([0-2][0-9])|(3[01])))|(((0[46
9])|([469])|(11))[\/](([1-9])|([0-2][0-9])|(30)))|((2|02)[\/](([1-9])|([0-2]
[0-9]))))[\/]\d{4}(1|01|2|02|3|03|4|04|5|05|6|06|7|07|8|08|9|09|10|11|12{1,2
}):(([0-5]{1}[0-9]{1}\s{0,1})([AM|PM|am|pm]{2,2}))\W{0}$

I'd like the format to be
MM/DD/YYYY HH:MM AM/PM
-the time is optional for the user to enter
-the date is mandatory
-I'd like to be able to accept either 5/1/2002 or 05/01/2002 as the date

So, if anyone could help me out with that, I'd really appreciate it.
Thanks
--
Melissa Whalen, BBA/IS, AIT

Reply to this message...
 
    
Darren Neimke
Melissa,

I'll take a look at this later when I have some time - if somebody
doesn't solve it for you by then. But I'd just like to make a comment
on the pattern that you've defined... Take a look at it; it's a huge,
fat, hairy, unweildy beast! Do you agree? What would you do if you
stumbled across a page that another developer had written and it had
something like this placed in the middle of it:

    ([13578])|(1[02]))[\/](([1-9])|([0-2][0-9])|(3[01])))|(((0[46
=09
9])|([469])|(11))[\/](([1-9])|([0-2][0-9])|(30)))|((2|02)[\/](([1-9])|([
0-2]
=09
[0-9]))))[\/]\d{4}(1|01|2|02|3|03|4|04|5|05|6|06|7|07|8|08|9|09|10|11|12
{1,2
    }):(([0-5]{1}[0-9]{1}\s{0,1})([AM|PM|am|pm]{2,2}))\W{0}$

What I try to do in such a case is to break the pattern down into
sub-patterns ( if possible ). In your case, that might mean defining a
"Date" pattern, and a "Time" pattern:

var RE_DATE =3D "\d{2}[\/-]\d{2}[\/-]/\d{4}"
var RE_TIME =3D "\d{1,2}\:\d{1,2} [aApP]M"
var RE_DATETIME =3D RE_DATE + ' ' + RE_TIME

Just a thought :-)

Regards,

Darren

-----Original Message-----
From: Melissa Whalen [mailto:Click here to reveal e-mail address]=20
Sent: Tuesday, May 14, 2002 6:39 AM
To: aspngregexp
Subject: [aspngregexp] Date + Time Validator

Hi,

I'm trying to get a regex that will validate a date and time. The date
has to be there, but the time can be an optional entry. I've looked
through regexlib.com, but I can't seem to find exactly what I'm looking
for. I've tried combining a couple to get what I need, but I'm not sure
of all the syntax so I wasn't able to get it to work. Here's what I've
tried (taken from regexlib.com):
^((((0[13578])|([13578])|(1[02]))[\/](([1-9])|([0-2][0-9])|(3[01])))|(((
0[46
9])|([469])|(11))[\/](([1-9])|([0-2][0-9])|(30)))|((2|02)[\/](([1-9])|([
0-2]
[0-9]))))[\/]\d{4}(1|01|2|02|3|03|4|04|5|05|6|06|7|07|8|08|9|09|10|11|12
{1,2
}):(([0-5]{1}[0-9]{1}\s{0,1})([AM|PM|am|pm]{2,2}))\W{0}$

I'd like the format to be
MM/DD/YYYY HH:MM AM/PM
-the time is optional for the user to enter
-the date is mandatory
-I'd like to be able to accept either 5/1/2002 or 05/01/2002 as the date

So, if anyone could help me out with that, I'd really appreciate it.
Thanks
--
Melissa Whalen, BBA/IS, AIT

| [aspngregexp] member Click here to reveal e-mail address =3D YOUR ID=20
| http://www.asplists.com/asplists/aspngregexp.asp =3D JOIN/QUIT=20
| http://www.asplists.com/search =3D SEARCH Archives

Reply to this message...
 
    
Darren Neimke
Melissa,

Sorry, I don't actually have the framework installed here to do a test,
so I've whipped up a javascript demo version - please let me know if you
have trouble reading javascript.

Anyways, I wanted to create this demo to give you an idea of what I was
talking about before. Save it as "RE_Reuseable.html" and fire it up in
a browser to see it in action:

------------------------------ RE_Reuseable.html -------------------

<HTML><HEAD>
<SCRIPT LANGUAGE=3D"javascript">

var RE_DATE                =3D
"(0[1-9]|[12][0-9]|3[01])+[\/-](0[1-9]|1[0-2])+[\/-](200[2-9]|[1-9][0-9]
)"
var RE_TIME                =3D
"(0?[1-9]|1[012])+\:(0?[0-9]|[12345][0-9]) [aApP]M"
var RE_DATETIME            =3D '^(' + RE_DATE + ' ' + RE_TIME + ')$'
var RE_DATETIME_optEITHER    =3D '^(' + RE_DATE + '|' + RE_TIME + ')$'
var RE_DATETIME_optTIME        =3D '^(' + RE_DATE + ' ' + RE_TIME + '|' +
RE_DATE + ')$'
var RE_DATETIME_optDATE        =3D '^(' + RE_DATE + ' ' + RE_TIME + '|' +
RE_TIME + ')$'

function ApplyPattern( str )
{
var result
var reDateTime
=09
// matching just the date
reDateTime =3D new RegExp( "^" + RE_DATE + "$" )
result =3D reDateTime.test( str )
alert( "Using " + RE_DATE + "\nreturned: " + result )
=20
reDateTime =3D null
=20
// matching just the time
reDateTime =3D new RegExp( RE_TIME )
result =3D reDateTime.test( str )
alert( "Using " + RE_TIME + "\nreturned: " + result )
=20
reDateTime =3D null
=09
// matching the date and the time
reDateTime =3D new RegExp( RE_DATETIME )
result =3D reDateTime.test( str )
alert( "Using " + RE_DATETIME + "\nreturned: " + result )
=20
reDateTime =3D null
=09
// matching the date with the time being optional
reDateTime =3D new RegExp( RE_DATETIME_optTIME )
result =3D reDateTime.test( str )
alert( "Using " + RE_DATETIME_optTIME + "\nreturned: " + result )
}
</SCRIPT>
</HEAD><BODY>
<form>
<input type=3D"text" name=3D"DateTime" value=3D"" />
<input type=3D"button" value=3D"[ Test ]" onClick=3D"ApplyPattern(
this.form.DateTime.value ) ;" /> </form> </BODY> </HTML>

Reply to this message...
 
    
Darren Neimke
Just to take the whole concept of modularization 1 step further, I think
that something like the patterns below help to make Regular Expressions
*much* more maintainable:

--------------------------------- Modular_REs.html --------------------

// Define Date Class members...
var RE_DATE_DAY                =3D "(0[1-9]|[12][0-9]|3[01])"
// any day between 01 and 31
var RE_DATE_MONTH                =3D "(0[1-9]|1[0-2])"
// any month between 01 and 12
var RE_DATE_YEAR                =3D
"(200[2-9]|[1-9][0-9])"    // any time between 1900 and 2009
var RE_DATE_SEPARATOR            =3D "[\/-]"
// accept either "/" or "-"

// Define Time Class members...
var RE_TIME_HOUR        =3D "(0?[1-9]|1[012])"        // hour
between 1 and 12, if < 10, optional leading zero
var RE_TIME_MINUTE    =3D "(0?[0-9]|[12345][0-9])"    // min between 0
and 59, if < 10, optional leading zero
var RE_TIME_AMPM        =3D "[aApP][Mm]"            //
accept any casing combination of am|pm
var RE_TIME_SEPARATOR    =3D "\:"                // separate
hours and minutes with the ":" char

// Create derived expressions...
var RE_DATE                =3D RE_DATE_DAY +
RE_DATE_SEPARATOR + RE_DATE_MONTH + RE_DATE_SEPARATOR + RE_DATE_YEAR
var RE_TIME                =3D RE_TIME_HOUR +
RE_TIME_SEPARATOR + RE_TIME_MINUTE + ' ' + RE_TIME_AMPM
var RE_DATETIME            =3D '^(' + RE_DATE + ' ' + RE_TIME + ')$'
var RE_DATETIME_optEITHER    =3D '^(' + RE_DATE + '|' + RE_TIME + ')$'
var RE_DATETIME_optTIME        =3D '^(' + RE_DATE + ' ' + RE_TIME + '|' +
RE_DATE + ')$'
var RE_DATETIME_optDATE        =3D '^(' + RE_DATE + ' ' + RE_TIME + '|' +
RE_TIME + ')$'

Reply to this message...
 
 
System.DateTime




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification