DEV Community

Ajay Ojha
Ajay Ojha

Posted on

4 2

Decimal Number validation and apply the Currency Format in Angular

We discuss how we can validate the decimal number and also format the number as per the locale of the application.

We usually create a custom validator for numeric validation and the custom directive to format the number(120,875,256.25).

But here, we do not create any custom validator/directive to fulfill the need.

We use RxwebValidators to validate the decimal value as well as format the number.
RxwebValidator is available through @rxweb/reactive-form-validators package, this provides a lot of validators to fulfill the application needs, But we only discuss the numeric validator.

We have to install the package of @rxweb/reactive-form-validators to avail the services of RxwebValidators.

npm install @rxweb/reactive-form-validators

Once it's installed, we have to register the 'RxReactiveFormsModule' in the root module.

Let's do that.

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule,ReactiveFormsModule } from '@angular/forms';
import { AppComponent } from './app.component';

import {  RxReactiveFormsModule } from "@rxweb/reactive-form-validators"
@NgModule({
  imports:      [ BrowserModule, FormsModule,ReactiveFormsModule,RxReactiveFormsModule ],
  declarations: [ AppComponent],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

Now, We have to create the FormGroup as per the standard approach and we apply the numeric validator on the amount FormControl.

        this.userInfoFormGroup = this.formBuilder.group({
            amount:['', RxwebValidators.numeric()], 
        });

As per the above code, we have applied the numeric validator on the amount FormControl. This will validate the numeric value, if someone enters the value other than numeric then the FormControl will be invalid.

But wait, our need is to validate the decimal value as well and format the number. For that, we have to pass two parameters in the numeric validator.

        this.userInfoFormGroup = this.formBuilder.group({
            amount:['', RxwebValidators.numeric({allowDecimal:true,isFormat:true})], 
        });

allowDecimal property allows to user to enter the decimal value as well and isFormat is for format the numeric value as per the application locale. The format will be applied once the user moves to the another control.

Here is the complete code of the component.

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder } from "@angular/forms"
import { RxwebValidators } from '@rxweb/reactive-form-validators';

@Component({
    selector: 'app-numeric-add-validator',
    templateUrl: './numeric-add.component.html'
})
export class NumericAddValidatorComponent implements OnInit {
    userInfoFormGroup: FormGroup

    constructor(
        private formBuilder: FormBuilder )
    { }

    ngOnInit() {
        this.userInfoFormGroup = this.formBuilder.group({
            amount:['', RxwebValidators.numeric({allowDecimal:true,isFormat:true})], 
        });
    }
}

All Done!.

You can refer to the stackblitz example of the numeric validator.

Binds the HTML as per the standard practice of angular reactive forms.

If you have any question/suggestion, Please share your comments below.

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay