Home > Software design >  How to convert a comma to dot automatically?
How to convert a comma to dot automatically?

Time:05-23

When the user enters a comma in the INPUT, I would like the comma to be automatically transformed in a dot. Is it possible?

<label>Number </label>
<input type="number" maxlength="5" />

Here is a reproduction -> Link

CodePudding user response:

<input> elements of type number are used to let the user enter a number. They include built-in validation to reject non-numerical entries.

The user cann't input comma in the INPUT, so first you have to change input type as text. after that on onkeyup event you can replace the value like below:

<input type="text" onkeyup="this.value=this.value.replace(/,/g, '.');"/>

https://stackblitz.com/edit/angular-ivy-hc3mn8?file=src/app/app.component.html

CodePudding user response:

Using Angular Form Controls will automatically convert it to a correct number :

https://stackblitz.com/edit/angular-ivy-uewxfs?file=src/app/app.component.html,src/app/app.component.ts

  • Related