angular - Custom validation for positive numbers -
i trying find information, if there built-in validators, check if input positive number?
i trying build following:
static nonzero(control:control) { if (number(control.value) < 0) { control.seterrors({nonzero: true}) } else { control.seterrors(null) } }
however, didn't know how use in form builder:
this.form = _formbuilder.group({ field:['', validators.required]})
what doing wrong?
you can configure way leveraging validators.compose
method:
this.form = _formbuilder.group({ field:['', validators.compose([ validators.required, nonzero ]) ]});
this allows define 2 synchronous validators field.
edit
i implement validator way:
static nonzero(control:control):{ [key: string]: any; } { if (number(control.value) < 0) { return {nonzero: true}; } else { return null; } }
Comments
Post a Comment