Home > Web Front-end > JS Tutorial > body text

js restricts the text box to only input numbers (regular expression)_form effects

WBOY
Release: 2016-05-16 17:51:54
Original
1646 people have browsed it

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.

Copy code The code is as follows:

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:
Copy the code The code is as follows:



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.
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!