2017-01-10 63 views
0

我採用了棱角分明1.5表單生成器,我想在我的json文件添加validation我 1形式建設者驗證模式:角日期

{ 
    "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工作?以防錯誤解決。

回答

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

+0

不完全是我所要求的。 –

0

於是我就跳過了形式建設者驗證(它有很多問題),並使用ng-pattern

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

感謝。