JSFiddle - React, Tailwind, and code Playground

HTML

Valeur : <input type="text" id="test" name="test" onKeyUp="ajout_espace()">

JavaScript

function ajout_espace(){
nombre=document.getElementById("test").value;

if(nombre.charAt(0)!='.' && nombre.charAt(0)!=',' && nombre.charAt(nombre.length)!=',' &&  nombre.charAt(nombre.length-1)!='.')
{
nombre=document.getElementById("test").value.replace(/[^0-9,.]/g, '').replace(/[,]/,'.');
document.getElementById("test").value=new Intl.NumberFormat().format(nombre);
}

}