Skip to content

Instantly share code, notes, and snippets.

@lancevo
Last active August 4, 2016 19:49
Show Gist options
  • Save lancevo/bedaec7c7dfbecd9cb37544e85038553 to your computer and use it in GitHub Desktop.
Save lancevo/bedaec7c7dfbecd9cb37544e85038553 to your computer and use it in GitHub Desktop.
A validator to validator range of number
import {AbstractControl, ValidatorFn} from '@angular/forms';
export function validateNumberRange(min: number, max:number): ValidatorFn {
return (control: AbstractControl): { [key:string]: any } => {
var value = parseInt(control.value,10);
return (isNaN(value) || (value < min) || (value > max)) ?
{'numberRange' : {'min': min, 'max':max, 'actualValue': value}} :
null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment