Javascript controls how to enter only numbers and decimal places in the text box
This article describes how to enter only numbers and decimal places in the javascript control text box. The example shows how to use a regular expression in javascript to restrict numbers and decimal places, for more information, see
This article describes how to enter only numbers and decimal places in the Javascript control text box. Share it with you for your reference. The specific implementation method is as follows:
The Code is as follows:
Function clearNoNum (obj ){
Obj. value = obj. value. replace (/[^ \ d.]/g, ""); // clear characters other than "Number" and "."
Obj. value = obj. value. replace (/^ \./g, ""); // verify that the first character is a number instead of a number.
Obj. value = obj. value. replace (/\. {2,}/g, "."); // only keep the first. Clear unnecessary.
Obj. value = obj. value. replace (". "," $ # $ "). replace (/\. /g ,""). replace ("$ # $ ",". ");
}
The usage is as follows:
The Code is as follows:
<Input name = "input1" onkeyup = "clearNoNum (this)">
I hope this article will help you design javascript programs.