我有一个自定义验证器,用于检查输入字段是否输入了数字。
![在此输入图片描述](https://istack.dev59.com/K0J6Q.webp)
代码如下:
import { AbstractControl, ValidatorFn } from '@angular/forms';
export class NumberValidators {
static isNumberCheck(): ValidatorFn {
return (c: AbstractControl): {[key: string]: boolean} | null => {
if (c.value !== undefined && (isNaN(c.value))) {
return { 'value': true };
}
return null;
};
}
}
但是我的问题是,当我输入一个小数时,它会引发验证标志,而我不想要这样的结果。我希望输入字段可以包含整数和小数。请有经验的朋友帮忙解决一下。
以下是我使用parsleyjs进行验证的模板代码。
<div class="form-group row">
<label for="upperbeltposition" class="col-md-6 col-form-label">Upper Belt Position (mm) <span class="required">*</span></label>
<div class="col-md-5">
<input class="form-control input-transparent " id="upperbeltposition" type="text" formControlName="upperbeltposition" data-parsley-trigger="blur"
required="required" data-parsley-type="number"/>
</div>
</div>
<div class="form-group row">
<label for="platformxposition" class="col-md-6 col-form-label">Platform X Position (mm) <span class="required">*</span></label>
<div class="col-md-5">
<input class="form-control input-transparent " id="platformxposition" type="text" formControlName="platformxposition" data-parsley-trigger="blur"
required="required" data-parsley-type="number"/>
</div>
</div>
<div class="form-group row">
<label for="platformyposition" class="col-md-6 col-form-label">Platform Y Position (mm) <span class="required">*</span></label>
<div class="col-md-5">
<input class="form-control input-transparent " id="platformyposition" type="text" formControlName="platformyposition" data-parsley-trigger="blur"
required="required" data-parsley-type="number"/>
</div>
</div>
我正在使用响应式表单来编写我的组件,以下是我的代码:
upperbeltposition: ['', [Validators.required, NumberValidators.isNumberCheck]],
platformxposition: ['', [Validators.required, NumberValidators.isNumberCheck]],
platformyposition: ['', [Validators.required, NumberValidators.isNumberCheck]]
thanks!!