Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reg Exp test for dd/mm/yy

Status
Not open for further replies.

TelcoBT

Programmer
Apr 24, 2003
45
GB
Hi,

I'm new to regular expressions, and I can't see the problem with my code below.

I've been testing a function that uses regular expressions to validate dd/mm/yyyy and dd/mm/yy format as part of date validation (doesn't validate days per month, these are handled later)

but the dd/mm/yy reg exp doesn't work as expected.

below are the 2 reg expressions, and the dd/mm/yyyy always seems to work, but the dd/mm/yy allows dates of dd/mm/yyy


var strRE_ddmmyyyy = /([0,1,2]\d|[3][0,1])\/([0][^0]|[1][0,1,2])\/\d{4}/;

var strRE_ddmmyy = /([0,1,2]\d|[3][0,1])\/([0][^0]|[1][0,1,2])\/\d{2}/;


The test part of my code is shown below

var testDate = strRE_ddmmyyyy.exec(strDateReq);

//if null is returned, the date is in an incorrect format
if ( testDate == null)
// so test for dd/mm/yy
testDate = strRE_ddmmyyyy.exec(strDateReq);
if ( testDate == null)
// date not dd/mm/yyyy or dd/mm/yy



Now the problem is if I supply a date of format dd/mm/yyy
the dd/mm/yyyy test works correctly, returning null, BUT the dd/mm/yy test does not return null, and says the date format is OK. I thought that the \/\d{2} part of the regular expression meant that there could only be 2 digits following the final forward slash, rather that 'at least 2' which seems to be the interpretation. Has anyone a view on this matter?

TIA

John


 
Problem solved, my apologies for wasting time, I misunderstood the definition, experimenting has made it clearer.

the sections of the expression

\/d{4}\ and \/d{2}
are the problem, I read the definition of the {n} brackets, which says 'occurs exactly n times', as meaning EQUALS, so I thought that

'dddd' and 'dd' were exact matches, and that anything else would fail, in fact it is more akin to INSTR, in that if
'ddd', the dd/mm/yyyy test fails because the 'ddd' doesn't hold 'dddd' BUT it does hold 'dd' hence the hence the dd/mm/yy test passing.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top