Itsik Mauyhas
Itsik Mauyhas

Reputation: 3994

Angular 1 form-builder validation pattern for date

I am using Angular 1.5 form builder and I want to add validation to my input-date in my json file:

{
    "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.There is an error in my json file - Invalid escape character in string.

2.This regex will work in form builder? in case the error will resolved.

Upvotes: 0

Views: 174

Answers (2)

Itsik Mauyhas
Itsik Mauyhas

Reputation: 3994

So I just skipped the form-builder validation(It has many issues) and use ng-pattern:

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

Thanks.

Upvotes: 0

Eduardo Lynch Araya
Eduardo Lynch Araya

Reputation: 824

Validate Date (Day: 1-31 / Month: 1-12 / Year: 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 group with day / month / year)

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

Result:

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

Good / Bad:

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

Try Here: https://regex101.com/r/WpgU9W/2

Upvotes: 1

Related Questions