[Solved] Looking for a regex to validate Cuban identity card


Note: this is uses rough date validation via pure RegEx (ie. any month can have up to 31 days):

[0-9]{2}(?:0[0-9]|1[0-2])(?:0[1-9]|[12][0-9]|3[01])[0-9]{5}

You can test if a string matches via JavaScript like so:

/[0-9]{2}(?:0[0-9]|1[0-2])(?:0[1-9]|[12][0-9]|3[01])[0-9]{5}/.test('82061512345');
// returns true because it is valid

If you need true date validation I would do something like the following:

var id1 = '82061512345'; // example valid id
var id2 = '82063212345'; // example invalid id

function is_valid_date(string) {
    var y = id.substr(0,2); // 82 (year)
    var m = id.substr(2,2); // 06 (month)
    var d = id.substr(4,2); // 15/32 (day)
    if (isNaN(Date.parse(y + '-' + m + '-' + d)) {
        return false;
    } else {
        return true;
    }
}

is_valid_date(id1); // returns true
is_valid_date(id2); // returns false

And you can tack on the following for full id validation:

function is_valid_id(id) {
    if (/[0-9]{11}/.test(id) && is_valid_date(id)) {
        return true;
    } else {
        return false;
    }
}

is_valid_id(id1); // returns true
is_valid_id(id2); // returns false

1

solved Looking for a regex to validate Cuban identity card