1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

How to restrict the US amount Regex pattern currency input field from accepting alphabets...

Discussão em 'Angular' iniciado por Nancy, Outubro 7, 2024 às 12:43.

  1. Nancy

    Nancy Guest

    I have the below RegEx wherein the amount entered by user is transformed into US currency format. The amount is getting transformed into US currency without any issues. But the input also accepts alphabets(which is actually not visible on screen) , so when i try to angular binding, the alphabets are visible. I have a solution where i can use keydown event and restrict alphabets using keyCode, but I dont want to create a separate function instead I want to modify the same Regex. How can i modify the same regex so that alphabets are also restricted. Any help is much appreciated.

    <input type="text" (input)="validateInput($event)" (blur)="transformAmount($event)">

    transformAmount(event:any){
    this.form.controls[getFormControlName].setValue(this.currencyPipe.transform((this.form.controls[getFormControlName].value.replace(/\$+/g,'').replace(/,/g,''))));
    }

    validateInput(event:any) {
    event.target.value=
    event.target.value.replace
    (/[^0-9.]/g,'').replace(/^\.$/g,'').replace(/(\..*?)\..*/g,'$1'.replace(/(\.\d{2}).+/g,'$1')
    }

    Continue reading...

Compartilhe esta Página