js controls the way text boxes enter only Numbers and decimal points

  • 2020-05-12 02:15:22
  • OfStack

This example shows how js controls text boxes to enter only Numbers and decimal points. Share with you for your reference. The specific implementation method is as follows:

function clearNoNum(obj) {
 obj.value = obj.value.replace(/[^\d.]/g, "");// Clear "Numbers" and" . Characters other than"
 obj.value = obj.value.replace(/^\./g, "");// Verify the first 1 Is a number rather than a character .
 obj.value = obj.value.replace(/\.{2,}/g, ".");// Keep only the first 1 a . Get rid of excess .
 obj.value = obj.value.replace(".", "$#$").replace(/\./g,"").replace("$#$", ".");
}

It can be used as follows:

<input name="input1" onkeyup="clearNoNum(this)">

I hope this article has been helpful to your javascript programming.


Related articles: