Javascript validation can only enter Numbers and an example of a decimal point

  • 2020-03-26 21:33:56
  • OfStack

function gaga(obj){ //Value allows you to enter a decimal point and number
obj.value = obj.value.replace(/[^d.]/g,""); //So let's replace everything that's not a number, except the sum.
obj.value = obj.value.replace(/^./g,""); //You have to make sure that the first one is a number and not a number.
obj.value = obj.value.replace(/.{2,}/g,"."); //Make sure there's only one. No more.
obj.value = obj.value.replace(".","$#$").replace(/./g,"").replace("$#$","."); //Guarantee. Only appear once, but can not appear more than twice


<input id="gaga" type="text" onkeyup="gaga(this)" /> 

Related articles: