js限制input输入

1.取消按钮按下时的虚线框,在input里添加属性值 hideFocus 或者 HideFocus=true
<input type="submit" value="提交" hidefocus="true" />

2.只读文本框内容,在input里添加属性值 readonly
<input type="text" readonly />

3.防止退后清空的TEXT文档(可把style内容做做为类引用)
<input type="text" style="behavior:url(#default#savehistory);" />

4.ENTER键可以让光标移到下一个输入框
<input type="text" onkeydown="if(event.keyCode==13)event.keyCode=9" />

5.只能为中文(有闪动)
<input type="text" onkeyup="value=value.replace(/[ -~]/g,‘‘)" onkeydown="if(event.keyCode==13)event.keyCode=9" />

6.只能为数字(有闪动)
<input type="text" onkeyup="value=value.replace(/[^\d]/g,‘‘) " onbeforepaste="clipboardData.setData(‘text‘,clipboardData.getData(‘text‘).replace(/[^\d]/g,‘‘))" />

7.只能为数字(无闪动)
<input type="text" style="ime-mode:disabled" onkeydown="if(event.keyCode==13)event.keyCode=9" onkeypress="if ((event.keyCode<48 || event.keyCode>57)) event.returnValue=false" />

8.只能输入英文和数字(有闪动)
<input type="text" onkeyup="value=value.replace(/[\W]/g,‘‘)" onbeforepaste="clipboardData.setData(‘text‘,clipboardData.getData(‘text‘).replace(/[^\d]/g,‘‘))" />

9.屏蔽输入法
<input type="text" name="url" style="ime-mode:disabled" onkeydown="if(event.keyCode==13)event.keyCode=9" />

10.只能输入 数字,小数点,减号(-) 字符(无闪动)
<input onkeypress="if (event.keyCode!=46 && event.keyCode!=45 && (event.keyCode<48 || event.keyCode>57)) event.returnValue=false" />

11.只能输入两位小数,三位小数(有闪动)
<input type="text" maxlength="9" onkeyup="if(value.match(/^\d{3}$/))value=value.replace(value,parseInt(value/10)) ;value=value.replace(/\.\d*\./g,‘.‘)" onkeypress="if((event.keyCode<48 || event.keyCode>57) && event.keyCode!=46 && event.keyCode!=45 || value.match(/^\d{3}$/) || /\.\d{3}$/.test(value)) {event.returnValue=false}" />

12.只能输入数字和小数点
<input type="text" size="12" onkeyup="this.value=this.value.replace(/[^\d\.]+?/g,‘‘)" />

13.只能输入数字和英文的
<input onkeyup="value=value.replace(/[\W]/g,‘‘) "onbeforepaste="clipboardData.setData(‘text‘,clipboardData.getData(‘text‘).replace(/[^\d]/g,‘‘))">

14.只能输入数字的
<input onkeyup="value=value.replace(/[^\d]/g,‘‘) "onbeforepaste="clipboardData.setData(‘text‘,clipboardData.getData(‘text‘).replace(/[^\d]/g,‘‘))">

15.只能输入全角的
<input onkeyup="value=value.replace(/[^\uFF00-\uFFFF]/g,‘‘)" onbeforepaste="clipboardData.setData(‘text‘,clipboardData.getData(‘text‘).replace(/[^\uFF00-\uFFFF]/g,‘‘))">

16.只能输入汉字的
<input onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,‘‘)" onbeforepaste="clipboardData.setData(‘text‘,clipboardData.getData(‘text‘).replace(/[^\u4E00-\u9FA5]/g,‘‘))">
时间: 2024-07-31 14:35:11

js限制input输入的相关文章

正则及JS限制input输入类型

1.只能输入和粘贴汉字 <input onkeyup="value=value.replace(/[^u4E00-u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^u4E00-u9FA5]/g,''))"><br/> 3.只能输入和粘贴数字 <input onkeyup="this

限制input输入类型(多种方法实现)

转自 http://blog.csdn.net/txqd1989/article/details/51697585 1.只能输入和粘贴汉字 <input onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))

input输入过滤js

html部分使用方式 <input  onkeyup="usrNameSet(this)" /> 其它的自己可以随便调用 Js部分 //只能输入数字.字母.小数点.汉字.@function usrNameSet(num){ var str=num.value;//var str = document.getElementById("userName").value; var value=str.replace(/[^\a-\z\A-\Z0-9\u4E00

正则表达式控制Input输入内容 ,js正则验证方法大全

https://blog.csdn.net/xushichang/article/details/4041507 正则表达式控制Input输入内容 2009年04月01日 17:15:00 阅读数:21747 不能输入中文 <input type="text" name="textfield"  onkeyup="this.value=this.value.replace(/[^/da-z_]/ig,'');"/>只能输入 数字和下划

【JS】input输入框只能输入数字

一.实现思路 input只能输入纯数字的思路其实很简单,监听输入框值的变化,每次输入检索输入框的值,将非数字的字段替换成空,再将此值赋予给输入框. 关键代码: \d:匹配数字 ^/d:全文匹配非数字 replace(/[^/d]/g,''):全文匹配非数字,并替换成空. 那我们的实现代码为: var input = document.querySelector("#demo"); input.oninput = function (){ input.value = input.valu

input输入什么div图层显示什么

<html> <meta charset="utf-8" /> <style > .ppp{ width:600px; height:300px; background-color:pink; } </style> <input type="text" id="ttt" name="" value=""  maxlength="" 

移动端开发注册、登陆input常用事件(input输入文字触发事件)

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>移动端开发注册.登陆input常用事件(input输入文字触发事件)</title> <meta name="keywords" content="

input输入时使用正则表达式进行限制

<table class="table_std">     <tr>         <th scope="row">会员ID</th>         <td>             <input type="text" name="comId" size="30" onkeyup="value=value.replace(/[

【前端开发】限制input输入保留两位小数

<input type="text" name='amount' id="cash_num" placeholder="请输入金额" onkeyup="num(this)" size="9"> js: //限制input输入保留两位小数 function num(obj) { // obj.value = obj.value.replace(/[^\d.]/g, ""); /