日期的Angular 1表单构建器验证模式

时间:2017-01-10 14:02:07

标签: angularjs json regex formbuilder

我正在使用Angular 1.5表单构建器,我想在我的validation文件中向input-date添加json

{
    "description": "managersList_birth_date",
    "model": "birth_date",
    "type": "input-date",
    "name": "Bday",
    "maxDate": "today",
    "class": {
        "main_part_class": "col-xs-12 col-sm-6 col-md-6",
        "label_part_class": "",
        "control_part_class": ""
    },
    "validation": {
        "required": true,
        "pattern": "/^(\d{2})\/(\d{2})\/(\d{4})$/" //error in json

    }
}

1.我的json文件中存在错误 - Invalid escape character in string

2.这个正则表达式适用于form builder?如果错误将得到解决。

2 个答案:

答案 0 :(得分:1)

验证日期(日:1-31 /月:1-12 /年:1000 - 2999)

<强> JavaScript的:

const regex = /(^3[01]|^2[\d]{1})\/([0][0-9]|[1][012])\/([12]\d{3})/gm;
const str = `
22/09/1994 <-- GOOD
32/13/2000 <-- BAD
31/12/2004 <-- GOOD`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }

    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

<强> PHP:

$re = '/(^3[01]|^2[\d]{1})\/([0][0-9]|[1][012])\/([12]\d{3})/m';
$str = '22/09/1994 <-- GOOD
32/13/2000 <-- BAD
31/12/2004 <-- GOOD';

preg_match_all($re, $str, $matches);

// Print the entire match result
print_r($matches);

REGEXP :( 3组日/月/年)

(^3[01]|^2[\d]{1})\/([0][0-9]|[1][012])\/([12]\d{3})

<强>结果:

Full match  39-49   `31/12/2004`
Group 1.    39-41   `31`
Group 2.    42-44   `12`
Group 3.    45-49   `2004`

好/坏:

22/09/1994 <-- GOOD
32/13/2000 <-- BAD
31/12/2004 <-- GOOD

试试这里: https://regex101.com/r/WpgU9W/2

答案 1 :(得分:0)

所以我跳过了表单构建器验证(它有很多问题)并使用ng-pattern

ng-pattern='/^(0?[1-9]|[12][0-9]|3[01])\/(0?[1-9]|1[012])\/(19\d\d|20[12]\d)$/'

感谢。