Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 custom validation for numbers and decimal values

I have a custom validator that checks if an input field has a number entered into it. enter image description here

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!!

like image 945
ZAJ Avatar asked Jun 14 '17 03:06

ZAJ


3 Answers

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;
    };
  }
}
like image 112
Meme Composer Avatar answered Sep 22 '22 19:09

Meme Composer


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

like image 21
Kondal Avatar answered Sep 24 '22 19:09

Kondal


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]+$/)]]

like image 22
pbre Avatar answered Sep 24 '22 19:09

pbre