Home > Enterprise >  Input type number with comma React
Input type number with comma React

Time:09-23

I have a question about input type number in React. I need to display dot separator if user enter number with comma. Like this

11,2

Should convert to

11.2

How I can convert this number?I try

value.replace(/,/g, '.')

But this isn't working.I still see comma in my input.

PS: This how I handle input

<input
type="number"
placeholder='Input'
name="inputValue"
step="0.01"
inputMode="decimal"
id='inputValue'
min="0"
value={inputValue}
onChange={handleChange}
/>

And this is my handleChange function

const handleChange = e => {
let { name, value } = e.target;
value = value.replace(/,/g, '.');
setData(prevState => ({ ...prevState, [name]: value}));
    }

CodePudding user response:

Try this code:

const Number = "11,1";

console.log(Number.replace(/\,/, "."));


//Now your handleChange function will look like this:

const handleChange = e => {
  let { name, value } = e.target;
  value = value.replace(/\,/, ".");
  setData(prevState => ({ ...prevState, [name]: value}));
}

  • Related