寻找正则表达式来验证古巴身份证 [英] Looking for a regex to validate Cuban identity card

查看:337
本文介绍了寻找正则表达式来验证古巴身份证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个如何验证古巴身份证格式的身份证号码的例子。我正在寻找HTML5中的正则表达式验证。

I need an example of how to validate an identification number in the Cuban identification card format. I'm looking for the regex validation in html5.

格式描述:

The format description:

Date of Birth (yymmdd) and 5 digits
There are 11 total digits.

示例: 89103024100

推荐答案

注意:这是通过纯正RegEx使用粗略日期验证(即任何月份最多可以有31天):

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}

您可以测试一个字符串是否与JavaScript一样匹配:

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

这篇关于寻找正则表达式来验证古巴身份证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆