Regular expression date

What's wrong with this regular expression? It won't work

var patt = /[0-9]{2}/[0-9]{2}/[1-9]{4}/;

if(patt.test(document.getElementById('date').value) == false){
    document.getElementById("errorTxt").innerHTML = "Error";
    return false;
}

Answers


You need to escape the / in the middle.

var patt = /\d\d\/\d\d\/\d{4}/;

Also, why weren't you allowing 2012 to be a valid year?


You need to escape / in middle of pattern. Make them \/.


To use slashes within a regular expression, they must be escaped:

var patt = /[0-9]{2}\/[0-9]{2}\/[1-9]{4}/;

On a related note, you can use \d to represent any digit:

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

Well, how can you match year 2012 with [1-9]{4}, when there is 0 in yyyy?

Using regex /\d{2}\/\d{2}\/\d{4}/ you can split date to numbers, but...

You should use JavaScript Date Validation instead.


Need Your Help

xcode - I want to receive the device language

ios device http-accept-language

How can I get the iPhone device language by below method to server?

Increase width of <td>

html css

How can I increase the width of a single &lt;td&gt; to 100% of the table?