Regular Expression Help please

M

MrHelpMe

I need to create a regular expression for a date field that works only
in the following format MM/DD/YYYY with the / in the format. No other
format can be inputted into the field. I need 2 numbers for MM 2
Numbers for DD and 4 numbers for YYYY. If the users enter 1 number for
month, 1 for day he should get an alert. Thanks.

I have this code and thought it was working but it is not. Any help
would be great. Thanks

Code:
var RegExPattern = /(\d{1,2})\W(\d{1,2})\W(\d{4})/;
 
T

Tim Slattery

MrHelpMe said:
I need to create a regular expression for a date field that works only
in the following format MM/DD/YYYY with the / in the format. No other
format can be inputted into the field. I need 2 numbers for MM 2
Numbers for DD and 4 numbers for YYYY. If the users enter 1 number for
month, 1 for day he should get an alert. Thanks.

I have this code and thought it was working but it is not. Any help
would be great. Thanks

Code:
var RegExPattern = /(\d{1,2})\W(\d{1,2})\W(\d{4})/;

The {1,2} elements tell it to accept one or two of the preceding
element. You say you want only two digits. I'd do something like this:

var RegExPattern = !\d{2})/\d{2})/\d{4}!

Using ! to delimit the RE instead of /, since you have slashes within
the RE.
 
E

Evertjan.

Tim Slattery wrote on 15 mei 2007 in
microsoft.public.inetserver.asp.general:
The {1,2} elements tell it to accept one or two of the preceding
element. You say you want only two digits. I'd do something like this:

var RegExPattern = !\d{2})/\d{2})/\d{4}!

Using ! to delimit the RE instead of /, since you have slashes within
the RE.

No, that !! is not part of j[ava]script regex
and those strange leftover )s?

Try:

if (/^\d{2}\/\d{2}\/\d{4}$/.test(t)) .....

or

if (/^(\d\d\/\){2}\d{4}$/.test(t)) .....
 
T

Tim Slattery

No, that !! is not part of j[ava]script regex
and those strange leftover )s?

OOP didn't specify what language, this being an ASP group, I assumed
VBScript. Maybe the trailing semicolon should have meant something.

I looked at the MSDN documentation for REs, and didn't see anything
about delimiting them. They use the same example over and over, which
is a SUB getting an argument and simply using that argument for a
delimiter. No discussion of enclosing the pattern in quotes or using
slashes that I could find.

I did wonder about alternate delimiters. That works in Perl and other
Unix-like contexts, but I guess not here. IMHO, it would simplify some
things if it did.

The leftover parens were a mistake.
 
M

MrHelpMe

Evertjan. said:
No, that !! is not part of j[ava]script regex
and those strange leftover )s?

OOP didn't specify what language, this being an ASP group, I assumed
VBScript. Maybe the trailing semicolon should have meant something.

I looked at the MSDN documentation for REs, and didn't see anything
about delimiting them. They use the same example over and over, which
is a SUB getting an argument and simply using that argument for a
delimiter. No discussion of enclosing the pattern in quotes or using
slashes that I could find.

I did wonder about alternate delimiters. That works in Perl and other
Unix-like contexts, but I guess not here. IMHO, it would simplify some
things if it did.

The leftover parens were a mistake.

Hello again everyone and thanks for the replies. Evertjan, I tried
what you recommended and received errors saying t was undefined.
However, after looking at this again I did manage to get this to
work. I expanded on my reg expressions and came up with the
following. If anyone see's anything wrong with this that I may have
overlooked please let me know.

/(\d{2})\W(\d{1,2})\W(\d{4})/

Thanks again.
 
E

Evertjan.

MrHelpMe wrote on 16 mei 2007 in microsoft.public.inetserver.asp.general:
Hello again everyone and thanks for the replies. Evertjan, I tried
what you recommended and received errors saying t was undefined.
However, after looking at this again I did manage to get this to
work. I expanded on my reg expressions and came up with the
following. If anyone see's anything wrong with this that I may have
overlooked please let me know.

/(\d{2})\W(\d{1,2})\W(\d{4})/

It is wrong
1- if you specified the second number to have two characters

{2} in stead of {1,2}

try:

/(\d{2})\W(\d{2})\W(\d{4})/

2- if you specify not to have unneccesaty characters in the regex:

no need to use ()

try:

/\d{2}\W\d{2}\W\d{4}/


3- if you want only / as a delimiter

just escape the / by \/

try:

/\d{2}\/\d{2}\/\d{4}/

or:

/(\d\d\/\){2}\/\d{4}/

4- and because now the string 'qwert00/00/0000 asdfg'
will test true, you will have to insert start ^ and end $ markers:

try:

/^(\d\d\/\){2}\/\d{4}$/


=== As you specified jscript in this Asp NG

by using var and ;

I suggest you use:

if ( /^(\d\d\/\){2}\/\d{4}$/.test(str) ) doWhatYouWantIfTrue();

=== I suggest you use international valid date strings like

yyyy-mm-dd or yyyy/mm/dd

=== But if you want only real dates to test true,
look in the archive of comp.lang.javascript

for instance here [but all over that NG]:
<http://tinyurl.com/2hj4zw>
 
E

Evertjan.

Tim Slattery wrote on 16 mei 2007 in
microsoft.public.inetserver.asp.general:
No, that !! is not part of j[ava]script regex
and those strange leftover )s?

OOP didn't specify what language, this being an ASP group, I assumed
VBScript. Maybe the trailing semicolon should have meant something.
MrHelpMe wrote on 15 mei 2007 in microsoft.public.inetserver.asp.general:

The giveaway was the "var" plus that in this ASP NG I would primarily
expect vbscript or jscript.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top