Knitex
Knitex

Reputation: 225

Regular expressions for javascript

i need a regular expression for an expire date for a visa in the layout MM/DD/YYYY i used

var expire = /(\d{2})+\/(\d{2})+\/(\d{4})/

and i know \d is for digit and the {4} allows for exactly 4 digits so im not really sure what im doing wrong here. thanks for the help

Upvotes: 1

Views: 170

Answers (2)

xsh.7
xsh.7

Reputation: 6250

This one works for MM/DD/YYYY:

(0[1-9]|1[012])\/(0[1-9]|[12][0-9]|3[01])\/(19|20)\d\d

Or this one for DD/MM/YYYY:

(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(19|20)\d\d

Although, they do not cover dates like 31/02/2012...

Upvotes: 1

Sufian Latif
Sufian Latif

Reputation: 13356

It should be:

var expire = /\d{2}\/\d{2}\/\d{4}/;

The + signs are causing the problems.

Upvotes: 5

Related Questions