SoulieBaby
SoulieBaby

Reputation: 5471

Date Regular Expression

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

Upvotes: 1

Views: 2208

Answers (2)

David R Tribble
David R Tribble

Reputation: 12214

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.

Upvotes: 1

meder omuraliev
meder omuraliev

Reputation: 186762

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

Upvotes: 3

Related Questions