Home Web Front-end JS Tutorial Input box filters non-numeric js code_javascript skills

Input box filters non-numeric js code_javascript skills

May 16, 2016 pm 04:36 PM
Input box filter

HTML:

Copy code The code is as follows:


JS:

window.onload=function(e){
var text=document.getElementById("only"),pattern=/\d/,//pattern匹配字母上的数字键
pattern2=/(9[6-9])|(10[0-5])|3(7|9)/,//pattern2匹配小键盘上的数字键和左右方向键
EventHandle={},event=e||window.event;//一个处理事件的对象

      //当网页加载的时候,进行判断,对事件处理对象进行定义属性,这样对事件对象的方法只需要进行一次判断,以后的其他事件
      //处理程序里面不需要判断
if(event.preventDefault){
EventHandle.preventDefault=function(e){
e.preventDefault();
};
}else{
EventHandle.preventDefault=function(e){
e.returnValue=false;
}
}
text.onkeydown=function(e){
var event=e||window.event;//不同事件的事件对象不一样,这个event和最前面的event不相等
if(!pattern.test(String.fromCharCode(event.keyCode))&&event.keyCode!=8//keyCode=8是退格键,因为可以对输入的数字进行修改,所以退格和左右方向键不禁止
&&!pattern2.test(event.keyCode.toString())||event.shiftKey||
event.ctrlKey||event.metaKey){
EventHandle.preventDefault(event);//如果不用对象的这个方法,写成下面这样,也可以执行,不过每次按下键盘的时候都会进行一次判断
                               //这个是没有必要的,所以在页面加载的时候对事件处理对象定义一个方法,加载后对象的方法就已经是确定的了,以后时候就可以了

                                //if(event.preventDefault){
                                //event.preventDefault();
                                //}else{
                                //event.returnValue=false;
                                //}
}
}
}

Copy after login

Uncommented version:

window.onload=function(e){
var text=document.getElementById("only"),pattern=/\d/,
pattern2=/(9[6-9])|(10[0-5])|3(7|9)/,
EventHandle={},event=e||window.event;
if(event.preventDefault){
EventHandle.preventDefault=function(e){
e.preventDefault();
};
}else{
EventHandle.preventDefault=function(e){
e.returnValue=false;
}
}
text.onkeydown=function(e){
var event=e||window.event;
if(!pattern.test(String.fromCharCode(event.keyCode))&&event.keyCode!=8
&&!pattern2.test(event.keyCode.toString())||event.shiftKey||
event.ctrlKey||event.metaKey){
EventHandle.preventDefault(event);
}
}
}
Copy after login

In IE11, F12 opens the developer tools and you can select the IE version for debugging

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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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 open filtered duplicate files in Quark How to open filtered duplicate files in Quark Mar 01, 2024 am 11:25 AM

When using Quark Browser, there is a function to filter duplicate files. Some friends are not very familiar with this. Here I will introduce how to turn on this function. If you are interested, come and take a look with me. 1. First, click "Quark Browser" on your mobile phone to enter the interface, then click and select "Quark Network Disk" in the options in the middle of the page to open and enter. 2. Find "Backup Settings" in the lower part of the Quark network disk interface, and click to open it, as shown in the figure below: 3. Next, on the page you enter, there is a "Filter Duplicate Files", which is displayed behind it There is a switch button. Click the circular slider on it and set it to color to turn on this function. When you continue to back up files, duplicate files will be skipped to save network disk capacity.

Python implements XML data filtering and filtering Python implements XML data filtering and filtering Aug 09, 2023 am 10:13 AM

Python implements XML data filtering and filtering. XML (eXtensibleMarkupLanguage) is a markup language used to store and transmit data. It is flexible and scalable and is often used for data exchange between different systems. When processing XML data, we often need to filter and filter it to extract the information we need. This article will introduce how to use Python to filter and filter XML data. Import the required modules Before starting, we

How to use PHP functions to search and filter data? How to use PHP functions to search and filter data? Jul 24, 2023 am 08:01 AM

How to use PHP functions to search and filter data? In the process of developing using PHP, it is often necessary to search and filter data. PHP provides a wealth of functions and methods to help us achieve these operations. This article will introduce some commonly used PHP functions and techniques to help you search and filter data efficiently. String search Commonly used string search functions in PHP are strpos() and strstr(). strpos() is used to find the position of a certain substring in a string. If it exists, it returns

PHP and PHPMAILER: How to implement automatic filtering of mail sending? PHP and PHPMAILER: How to implement automatic filtering of mail sending? Jul 21, 2023 am 09:25 AM

PHP and PHPMAILER: How to implement automatic filtering of mail sending? In modern society, email has become one of the important ways for people to communicate. However, with the popularity and widespread use of email, the amount of spam has also shown an explosive growth trend. Spam emails not only waste users' time and network resources, but may also bring viruses and phishing behaviors. Therefore, when developing the email sending function, it becomes crucial to add the function of automatically filtering spam. This article will introduce how to use PHP and PHPMai

Form validation and filtering methods in PHP? Form validation and filtering methods in PHP? Jun 29, 2023 pm 10:04 PM

PHP is a scripting language widely used in web development, and its form validation and filtering are very important parts. When the user submits the form, the data entered by the user needs to be verified and filtered to ensure the security and validity of the data. This article will introduce methods and techniques on how to perform form validation and filtering in PHP. 1. Form validation Form validation refers to checking the data entered by the user to ensure that the data complies with specific rules and requirements. Common form verification includes verification of required fields, email format, and mobile phone number format.

PHP data filtering: handling date and time input PHP data filtering: handling date and time input Jul 28, 2023 pm 07:41 PM

PHP Data Filtering: Processing Date and Time Input Overview: When developing web applications, it is often necessary to process date and time data entered by the user. Since user input may contain various formats and errors, effective data filtering and validation are necessary to ensure data accuracy and security. This article explains how to use PHP to handle date and time input, and provides corresponding code examples. Filtering and validation principles: Before processing date and time inputs, you first need to determine the corresponding filtering and validation principles. Here are some common ones

PHP data filtering: How to prevent file upload vulnerabilities PHP data filtering: How to prevent file upload vulnerabilities Jul 30, 2023 pm 09:51 PM

PHP Data Filtering: How to Prevent File Upload Vulnerabilities The file upload function is very common in web applications, but it is also one of the most vulnerable to attacks. Attackers may exploit file upload vulnerabilities to upload malicious files, leading to security issues such as server system intrusion, user data being leaked, or malware spreading. In order to prevent these potential threats, we should strictly filter and inspect files uploaded by users. Verify file type An attacker may rename the .txt file to a .php file and upload

How to use PHP ZipArchive to filter and search files in compressed packages? How to use PHP ZipArchive to filter and search files in compressed packages? Jul 23, 2023 pm 08:34 PM

How to use PHPZipArchive to filter and search files in compressed packages? Overview In web development, we often need to process compressed package files, including filtering and searching. PHP provides the ZipArchive extension, which allows us to easily operate on compressed packages. This article will teach you how to use the PHPZipArchive extension to filter and search compressed archive files. Steps First, make sure your PHP environment has the ZipArchive extension enabled. you may

See all articles