Regular Expression Help please
Regular Expression Help please
am 15.05.2007 20:35:23 von clinttoris
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})/;
Re: Regular Expression Help please
am 15.05.2007 22:09:00 von Tim Slattery
MrHelpMe wrote:
>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.
--
Tim Slattery
MS MVP(DTS)
Slattery_T@bls.gov
http://members.cox.net/slatteryt
Re: Regular Expression Help please
am 16.05.2007 01:12:57 von exjxw.hannivoort
Tim Slattery wrote on 15 mei 2007 in
microsoft.public.inetserver.asp.general:
>>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.
>
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)) .....
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Re: Regular Expression Help please
am 16.05.2007 15:22:58 von Tim Slattery
"Evertjan." wrote:
>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.
--
Tim Slattery
MS MVP(DTS)
Slattery_T@bls.gov
http://members.cox.net/slatteryt
Re: Regular Expression Help please
am 16.05.2007 15:38:58 von clinttoris
On May 16, 9:22 am, Tim Slattery wrote:
> "Evertjan." wrote:
> >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.
>
> --
> Tim Slattery
> MS MVP(DTS)
> Slatter...@bls.govhttp://members.cox.net/slatteryt
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.
Re: Regular Expression Help please
am 16.05.2007 16:35:34 von exjxw.hannivoort
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
>>> Code:
>>> var RegExPattern = /(\d{1,2})\W(\d{1,2})\W(\d{4})/;
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]:
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Re: Regular Expression Help please
am 16.05.2007 16:39:35 von exjxw.hannivoort
Tim Slattery wrote on 16 mei 2007 in
microsoft.public.inetserver.asp.general:
> "Evertjan." wrote:
>
>
>>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:
> > Code:
> > var RegExPattern = /(\d{1,2})\W(\d{1,2})\W(\d{4})/;
The giveaway was the "var" plus that in this ASP NG I would primarily
expect vbscript or jscript.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)