Skip to content

Instantly share code, notes, and snippets.

@CharlieGreenman
Created September 20, 2019 12:34
Show Gist options
  • Save CharlieGreenman/df0208ea1f96739c122bfe246b4f02c4 to your computer and use it in GitHub Desktop.
Save CharlieGreenman/df0208ea1f96739c122bfe246b4f02c4 to your computer and use it in GitHub Desktop.
Number validator file
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