


js restricts the text box to only input numbers (regular expression)_form effects
May 16, 2016 pm 05:51 PMOkay, 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:
<input type="text" name="txtFee" id="txtFee" class="common" onkeyup="chkPrice(this);" onblur="chkLast(this)" onpaste="javascript: return false;"/>
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.

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to set a picture as the background in OneNote

iOS 17: How to change iPhone clock style in standby mode

Generate random numbers and strings in JavaScript

Find numbers that are not divisible by any number in a range, using C++

C++ program to round a number to n decimal places

Use C++ to write code to find the Nth non-square number

Check if it is a number using is_numeric() function in PHP
