473,400 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

Regular Expression Help please

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})/;

May 15 '07 #1
6 1864
MrHelpMe <cl********@hotmail.comwrote:
>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)
Sl********@bls.gov
http://members.cox.net/slatteryt
May 15 '07 #2
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)
May 15 '07 #3
"Evertjan." <ex**************@interxnl.netwrote:

>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)
Sl********@bls.gov
http://members.cox.net/slatteryt
May 16 '07 #4
On May 16, 9:22 am, Tim Slattery <Slatter...@bls.govwrote:
"Evertjan." <exjxw.hannivo...@interxnl.netwrote:
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.

May 16 '07 #5
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)
May 16 '07 #6
Tim Slattery wrote on 16 mei 2007 in
microsoft.public.inetserver.asp.general:
"Evertjan." <ex**************@interxnl.netwrote:

>>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)
May 16 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
4
by: Buddy | last post by:
Can someone please show me how to create a regular expression to do the following My text is set to MyColumn{1, 100} Test I want a regular expression that sets the text to the following...
2
by: Brian Kitt | last post by:
I have a process where I do some minimal reformating on a TAB delimited document to prepare for DTS load. This process has been running fine, but I recently made a change. I have a Full Text...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
2
by: kieran | last post by:
Hi, I am using Visual Studio 2005 and am trying to use a Regular Expression Validator control. I have a drop down list which contains various names, the first one is "Please Select". I want...
3
by: Lucky | last post by:
hi guys, i'm practising regular expression. i've got one string and i want it to split in groups. i was trying to make one regular expression but i didn't successed. please help me guys. i'm...
3
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular...
10
by: Mike9900 | last post by:
Hello, I need a regular expression to match a currency with its symbol, for example Pound66.99 must return 66.99 or Pound(66.99) or Pound-66.99 or -66.99Pound return -66.99 or any other...
1
by: sunil | last post by:
Hi, Am writing one C program for one of my module and facing one problem with the regular expression functions provided by the library libgen.h in solaris. In this library we are having two...
3
by: =?Utf-8?B?VEo=?= | last post by:
Hi, I want to know how Regular Expression can be used in this situation. I want to replace some string in specific condition.. The condition is to replace string only if the string is NOT inside...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.