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

May 16, 2016 pm 05:51 PM
number text box

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:

<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.
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to set a picture as the background in OneNote How to set a picture as the background in OneNote May 14, 2023 am 11:16 AM

How to set a picture as the background in OneNote

iOS 17: How to change iPhone clock style in standby mode iOS 17: How to change iPhone clock style in standby mode Sep 10, 2023 pm 09:21 PM

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

Generate random numbers and strings in JavaScript Generate random numbers and strings in JavaScript Sep 02, 2023 am 08:57 AM

Generate random numbers and strings in JavaScript

How to make a calendar in Word How to make a calendar in Word Apr 25, 2023 pm 02:34 PM

How to make a calendar in Word

Find numbers that are not divisible by any number in a range, using C++ Find numbers that are not divisible by any number in a range, using C++ Sep 13, 2023 pm 09:21 PM

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

C++ program to round a number to n decimal places C++ program to round a number to n decimal places Sep 12, 2023 pm 05:13 PM

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

Use C++ to write code to find the Nth non-square number Use C++ to write code to find the Nth non-square number Aug 30, 2023 pm 10:41 PM

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

Check if it is a number using is_numeric() function in PHP Check if it is a number using is_numeric() function in PHP Jun 27, 2023 pm 05:00 PM

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

See all articles