1

I'm using a date field checker but I want to change the regex from DD-MM-YYYY to DD/MM/YYYY but I can't seem to get it working..

Here's the snippet of code:

"date": { "regex": "/^[0-9]{1,2}\-\[0-9]{1,2}\-\[0-9]{4}$/", "alertText": "* Invalid date, must be in DD/MM/YYYY format" }, 

I'm sure it's quite simple but I have no idea about regex.. I've tried:

/^[0-9]{1,2}\/\[0-9]{1,2}\/\[0-9]{4}$/ 

and

/^[0-9]{1,2}\\/\\[0-9]{1,2}\\/\\[0-9]{4}$/ 

but neither of them work for me..

3
  • 1
    Are you certain that you're starting from a known good point? It appears as though your original regex as stated above won't even match DD-MM-YYYY because you've got quite a few extra backslashes in there. Commented Sep 21, 2009 at 23:55
  • 1
    @SoulieBaby - since you're validating on the client, have you considered an interface that does not need the user to enter an entire day string, but rather only the individual componenents without the separator? Commented Sep 22, 2009 at 0:15
  • I'm not sure lol I don't know much about the whole regex thing.. might have to scrap the idea and use a date picker instead.. :/ Commented Sep 22, 2009 at 0:58

2 Answers 2

3
o = { "date": { "regex": /^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$/, "alertText": "* Invalid date, must be in DD/MM/YYYY format" } } o.date['regex'].test('02/12/2008')//true o.date['regex'].test('2009-02-02')// false o.date['regex'].test('03-04-2009')// false 
Sign up to request clarification or add additional context in comments.

2 Comments

More info please - like .. what did you try? Did you try my code by itself or did you try to embed it in your application? Silly question, but why is your regex wrapped around quotes? A regex literal doesnt need to be wrapped in quotes unless you're dynamically creating it with new RegExp.
They're all wrapped in quotes, it's using jquery.validationEngine.js
1

Or:

/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/ 

Or the full long form, which might help you understand what's going on:

/^[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9][0-9][0-9]$/ 

This assumes you want exactly two digits for DD and MM and exactly four for YYYY.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.