I have a custom validator that checks if an input field has a number entered into it.
The code looks like so:
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;
};
}
}
But the issue I have is when I enter a decimal value it is raising the validation flag which I dont want. I want the input field to have whole numbers and decimal numbers. Can somebody please help me get this correct.
Below is my template code using parsleyjs for validation
<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>
and in my Component I am using Reactive form and below is my code
upperbeltposition: ['', [Validators.required, NumberValidators.isNumberCheck]],
platformxposition: ['', [Validators.required, NumberValidators.isNumberCheck]],
platformyposition: ['', [Validators.required, NumberValidators.isNumberCheck]]
thanks!!
Something like this should work
import { AbstractControl, ValidatorFn } from '@angular/forms';
export class NumberValidators {
static isNumberCheck(): ValidatorFn {
return (c: AbstractControl): {[key: string]: boolean} | null => {
let number = /^[.\d]+$/.test(c.value) ? +c.value : NaN;
if (number !== number) {
return { 'value': true };
}
return null;
};
}
}
can use ng-pattern validation
ng-pattern="^[0-9]+(.[0-9]{0,2})?$"
can you please check once like this
<label for="upperbeltposition"
class="col-md-6 col-form-label"
name="number">
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"/>
<span
ng-show="submitForm && platformxposition
.number.$error.pattern">This value should be a valid number
</span>
</div>
You must add your form name. and input field name.$error.pattern
Instead of creating a custom validator, you can also simply use the Validators.pattern
https://angular.io/api/forms/Validators#pattern:
['', [Validators.required, Validators.pattern(/^[.\d]+$/)]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With