ISO DateTime Validation

Regex tester for ISO 8601 formatted date strings.

by Liam Talbot

HTML

<br /><br />

<input type="datetime" id="box1" value="2012-10-06T04:13:00+00:00" />
<input type="button" id="testBox1" value="test" /><br /><br />

<input type="datetime" id="box2" value="2015-01-14T11:00:00.000Z" />
<input type="button" id="testBox2" value="test" /><br /><br />

<input type="datetime" id="box3" value="2015-01-14T11:00:00" />
<input type="button" id="testBox3" value="test" /><br /><br />

CSS

#box1, #box2, #box3 {
    margin: 5px;
    width: 220px;
    height: 25px;
}
#testBox1, #testBox2, #testBox3 {
    margin: 5px;
    width: 50px;
    height: 25px;
}

JavaScript

$(document).ready(function () {
    var validationRegex = /^(\d{4})-(\d{2})-(\d{2})T(\d{2})\:(\d{2})\:(\d{2})(([+-](\d{2})\:(\d{2}))|(\.\d{3}Z))?$/;
    
    $('#testBox1').click(function () {
        if($('#box1').val().match(validationRegex))
            alert('ok');
        else
            alert('not ok');
    });
    $('#testBox2').click(function () {
        if($('#box2').val().match(validationRegex))
            alert('ok');
        else
            alert('not ok');
    });
    $('#testBox3').click(function () {
        if($('#box3').val().match(validationRegex))
            alert('ok');
        else
            alert('not ok');
    });
});