Created
March 27, 2016 15:51
-
-
Save masonkmeyer/73a4fe5e26c26d274846 to your computer and use it in GitHub Desktop.
Use a regular expression to validate Angular2 controls.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { AbstractControl } from 'angular2/common'; | |
export class RegexValidators { | |
static pattern(pattern: RegExp) : (c: AbstractControl) => {[key: string]: any } { | |
return (control: AbstractControl): { [key: string]: any } => { | |
let v: string = control.value; | |
return pattern.test(v) ? null : { "pattern": { "requiredPattern": `^${pattern}$`, "actualValue": v } }; | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The current Validators.patterns takes a string and wraps it like this:
This code just passes the expression straight through. Ideally, this should be union typed in Angular2 to accept either a RegExp or a string.