Home >Web Front-end >JS Tutorial >js restricts the text box to only input numbers (regular expression)_form effects
js restricts the text box to only input numbers (regular expression)_form effects
WBOYOriginal
2016-05-16 17:51:541689browse
Okay, let’s get down to business. By the way, I found many ways to limit the text box on the Internet, but none of them were satisfactory, so I had to practice it myself. Practice brings true knowledge. This sentence is correct.
function chkPrice(obj){ obj.value = obj.value.replace(/[^d.]/g,""); //Must ensure that the first digit is a number instead. obj.value = obj.value.replace(/^ ./g,""); //Guarantee that only one appears, not multiple. obj.value = obj.value.replace(/.{2,}/g,"."); //Guaranteed. It will only appear once and not more than twice obj.value = obj.value.replace(".","$#$").replace(/./g,"" ).replace("$#$","."); } function chkLast(obj){ // If illegal characters appear, intercept them 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 that needs to be called:
The last one: onpaste="javascript: return false;" is to disable the "Paste" menu of the right mouse button, which can prevent the right mouse button or Ctrl V from pasting illegal Data, hehe.
I have only tested the above JS in the IE8 environment. If you have any ideas or questions, please leave a message.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn