Now, let's get down to the truth. I found a lot of ways to restrict text boxes on the Internet, so I had to practice it on my own and practice it. That's right.
Copy codeThe Code is as follows:
Function chkPrice (obj ){
Obj. value = obj. value. replace (/[^ \ d.]/g ,"");
// Ensure that the first digit is a number instead of a number.
Obj. value = obj. value. replace (/^ \./g ,"");
// Ensure that there is only one and no more.
Obj. value = obj. value. replace (/\. {2,}/g ,".");
// Ensure that the message appears only once, but not more than twice.
Obj. value = obj. value. replace (". "," $ # $ "). replace (/\. /g ,""). replace ("$ # $ ",". ");
}
Function chkLast (obj ){
// Truncates an invalid character.
If (obj. value. substr (obj. value. length-1), 1) = '.')
Obj. value = obj. value. substr (0, (obj. value. length-1 ));
}
The following is the text box to be called:
Copy codeThe Code is as follows:
<Input type = "text" name = "txt0000" id =" txt0000" class = "common" onkeyup = "chkPrice (this);" onblur = "chkLast (this) "onpaste =" javascript: return false; "/>
At last, onpaste = "javascript: return false;" is used to disable the "Paste" menu by right-clicking the mouse to prevent the right-clicking or Ctrl + V from pasting invalid data.
I have tested the above JavaScript code only in the IE8 environment. If you have any ideas or questions, please leave a message.