Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > Regular Expression Help please

Reply
Thread Tools

Regular Expression Help please

 
 
MrHelpMe
Guest
Posts: n/a
 
      05-15-2007
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})/;

 
Reply With Quote
 
 
 
 
Tim Slattery
Guest
Posts: n/a
 
      05-15-2007
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)

http://members.cox.net/slatteryt
 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      05-15-2007
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)
 
Reply With Quote
 
Tim Slattery
Guest
Posts: n/a
 
      05-16-2007
"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)

http://members.cox.net/slatteryt
 
Reply With Quote
 
MrHelpMe
Guest
Posts: n/a
 
      05-16-2007
On May 16, 9:22 am, Tim Slattery <Slatter...@bls.gov> wrote:
> "Evertjan." <exjxw.hannivo...@interxnl.net> 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.

 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      05-16-2007
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]:
<http://tinyurl.com/2hj4zw>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      05-16-2007
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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seek xpath expression where an attribute name is a regular expression GIMME XML 3 12-29-2008 03:11 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57