Marco Jr
Marco Jr

Reputation: 6806

Angular 2 - NgForm with pattern issue

everyone ! The pattern itself works. I extracted from http://regexlib.com/RETester.aspx?regexp_id=409 and tested using https://regex101.com/ with the data: 02/02/2012 and many others.

Maybe I'm not using properly it ?

<form class="searchBlock" [ngFormModel]="formSearch">
   <input type="text" ngControl="frmDateFrom">
</form>
<span>{{formSearch.valid | json}}</span>


import {FORM_DIRECTIVES, FormBuilder, Control, ControlGroup, Validators} from 'angular2/common';
export class MyComponent {
  formSearch: ControlGroup;
  frmDateFrom: Control;
  constructor(private builder: FormBuilder) {
        this.frmDateFrom = new Control('', Validators.compose([Validators.required, Validators.pattern("^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((1[6-9]|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$")]));
        this.formSearch = builder.group({
        frmDateFrom: this.frmDateFrom
    });
  }
}

No matter what is the value, formSearch.valid always will show me false. What could be wrong ?

Upvotes: 2

Views: 216

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657871

If you use a simple regex like "^[a-z]$" it's working fine, therefore I'd assume it has to be the regex that causes the issues.

Plunker example

I'm pretty sure the problem is caused by the backslashes which need to be escapted. Just change any \ to \\

Upvotes: 2

Related Questions