本文主要介绍了JavaScript验证的相关知识整理。具有很好的参考价值。下面跟着小编一起来看下吧
1.文本框只能输入数字代码(小数点也不能输入)
<input onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')">
2.只能输入数字,能输小数点.
<input onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')"> <input name=txt1 onchange="if(/\D/.test(this.value)){alert('只能输入数字');this.value='';}">
3.数字和小数点方法二
<input type=text t_value="" o_value="" onkeypress="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/))this.value=this.t_value; else this.t_value=this.value;if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=this.value" onkeyup="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/))this.value=this.t_value;else this.t_value=this.value; if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=this.value" onblur="if(!this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?|\.\d*?)?$/))this.value=this.o_value;else{if(this.value.match(/^\.\d+$/))this.value=0+this.value; if(this.value.match(/^\.$/))this.value=0;this.o_value=this.value}">
4.只能输入字母和汉字
<input onkeyup="value=value.replace(/[\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[\d]/g,''))" maxlength=10 name="Numbers">
5.只能输入英文字母和数字,不能输入中文
<input onkeyup="value=value.replace(/[^\w\.\/]/ig,'')">
6.只能输入数字和英文chun
<input onKeyUp="value=value.replace(/[^\d|chun]/g,'')">
7.小数点后只能有最多两位(数字,中文都可输入),不能输入字母和运算符号:
<input onKeyPress="if((event.keyCode<48 || event.keyCode>57) && event.keyCode!=46 || /\.\d\d$/.test(value))event.returnValue=false">
8.小数点后只能有最多两位(数字,字母,中文都可输入),可以输入运算符号:
<input onkeyup="this.value=this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3')">
禁止特殊字符:
onKeyPress="if(event.keyCode < 45 || event.keyCode > 57 ) event.returnValue = false;"
只能输入汉字:
<input onkeyup="value=value.replace(/[^/u4E00-/u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^/u4E00-/u9FA5]/g,''))">
style="ime-mode:disabled"禁止汉字输入法
只能输入数字:
<input onkeyup="value=value.replace(/[^/d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^/d]/g,''))">
只能输入英文和数字:
<input onkeyup="value=value.replace(/[/W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^/d]/g,''))">
控制输入框只能输入文字或数字,也可以不允许输入特殊字符
这里不允许输入如下字符: (像 ^&* 等)
<textarea rows=2 cols=20 name=comments onKeypress="if ((event.keyCode > 32 && event.keyCode < 48) || (event.keyCode > 57 && event.keyCode < 65) || (event.keyCode > 90 && event.keyCode < 97)) event.returnValue = false;">
只禁止空格输入
onkeyup="value=value.replace(//s/g,'')" onkeydown="if(event.keyCode==32) return false"
只能输入中文和英文:
onkeyup="value=value.replace(/[^/a-zA-Z/u4E00-/u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^/a-zA-Z/u4E00-/u9FA5]/g,''))"
不允许输入特殊字符和空格:
<input id="code" onkeypress="return ValidateSpecialCharacter();" onblur="validate(this)"/>
不能为空
<input onblur="if(this.value.replace(/^ +| +$/g,'')=='')alert('不能为空!')">
判断字符由字母和数字,下划线,点号组成.且开头的只能是下划线和字母
/^([a-zA-z_]{1})([\w]*)$/g.test(str)
只能输入数字
<input name="text" type="text" id="NewPage" onKeyUp="value=value.replace(/\D/g,'')" onafterpaste="value=value.replace(/\D/g,'')" >
只能输入中文
<input type="text" onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')">
只能输入英文
<input type="text" onkeyup="value=value.replace(/[^\a-\z\A-\Z]/g,'')"> <input type="text" onkeyup="value=value.replace(/[^a-zA-Z]/g,'')">
只能输入中文、英文、数字、@符号和.符号
<input type="text" onkeyup="value=value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\@\.]/g,'')">
只允许输入英文,且不能粘贴也无法弹出粘贴菜单
<input type="text" onkeyup="value=value.replace(/[^\a-\z\A-\Z]/g,'')" onkeydown="fncKeyStop(event)" onpaste="return false" oncontextmenu = "return false"/>
只能输入数字和点号(注意:在[^\d\.]里的d不能写成大写D,否则就变成除了数字以外的所有字符)
<input name="price" type="text" size="8" maxlength="8" onkeyup="value=value.replace(/[^\d\.]/g,'')" >
总而言之:先在里输入onkeyup="value=value.replace(/[^\X]/g,'')" 然后在(/[\X]/g,'')里的X换成你想输入的代码就可以了
中文:u4E00-u9FA5
数字:d、0-9
英文:a-z、A-Z
其它符号@,点或其它符号.也可以多个,用\隔开就行了.
例如:
中、英文和数字加@符号加点符号:\a-\z\A-\Z0-9\u4E00-\u9FA5\@\.
若想在文本框里不能右键弹出菜单和不能粘贴进复制的信息的话就要在里输入 onKeyDown="fncKeyStop(event)" onpaste="return false" oncontextmenu="return false;"
其一,只允许输入数字和小数点。
<input onKeypress="return (/[/d.]/.test(String.fromCharCode(event.keyCode)))" style="ime-mode:Disabled">
其二,判断的更详细一些,甚至22..2这样不算数字也判断得出来
<script> function check(){ if (isNaN(tt.value)) {alert("非法字符!"); tt.value="";} } </script> <input type="text" name="tt" onkeyup="check();">
其三,只允许输入整数。其实也完全可以根据第三条来举一反三做一些限制。
<script language=javascript> function onlyNum() { if(!(event.keyCode==46)&&!(event.keyCode==8)&&!(event.keyCode==37)&&!(event.keyCode==39)) if(!((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105))) event.returnValue=false; } </script><input onkeydown="onlyNum();" style="ime-mode:Disabled>
结语,其实
style="ime-mode:Disabled
这句是比较实用的。意为关闭输入法。省得有些人开着全角输入数字,结果输入不进去来找你哭天抹泪的,还怪你设计的不好。
只允许输入数字
<input name="username" type="text" onkeyup="value=this.value.replace(//D+/g,'')">
只允许输入英文字母、数字和下划线(以下二种方法实现)
<input name="username" type="text" style="ime-mode:disabled"> <input name="username" type="text" onkeyup="value=value.replace(/[^/w/.//]/ig,'')">
只允许输入英文字母、数字和&=@
<input name="username" type="text" onkeyup="value=value.replace(/[^/w=@&]|_/ig,'')">
只允许输入英文字母、数字和&=@
<input name="username" type="text" onkeyup="value=value.replace(/[^/w=@&]|_/ig,'')">
只允许输入汉字
<input name="username" type="text" onkeyup="value=value.replace(/[^/u4E00-/u9FA5]/g,'')">
The above is the detailed content of JavaScript verification knowledge code summary sharing. For more information, please follow other related articles on the PHP Chinese website!

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.