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
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