Created
September 20, 2019 12:34
-
-
Save CharlieGreenman/df0208ea1f96739c122bfe246b4f02c4 to your computer and use it in GitHub Desktop.
Number validator file
This file contains hidden or 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, Validators, ValidatorFn } from '@angular/forms'; | |
function isPresent(obj: any): boolean { | |
return obj !== undefined && obj !== null; | |
} | |
export const number: ValidatorFn = (control: AbstractControl): {[key: string]: boolean} => { | |
if (isPresent(Validators.required(control))) return null; | |
let v: string = control.value; | |
return /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test(v) ? null : {'number': true}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment