订阅本栏目 RSS您所在的位置: 深山工作室 > HTML > 正文

文本框输入限制

深山行者个人网站 2007/12/22 23:30:59 深山行者 字体: 浏览 7008

style="word-break:break-all"

 

<script>
 function regInput(obj, reg, inputStr)
 {
  var docSel = document.selection.createRange()
  if (docSel.parentElement().tagName != "INPUT") return false
  oSel = docSel.duplicate()
  oSel.text = ""
  var srcRange = obj.createTextRange()
  oSel.setEndPoint("StartToStart", srcRange)
  var str = oSel.text + inputStr + srcRange.text.substr(oSel.text.length)
  return reg.test(str)
 }
</script>

小写英文:<xmp style= "display:inline"> </xmp>
<input onkeypress = "return regInput(this, /^[a-z]*$/,  String.fromCharCode(event.keyCode))"
  onpaste  = "return regInput(this, /^[a-z]*$/,  window.clipboardData.getData('Text'))"
  ondrop  = "return regInput(this, /^[a-z]*$/,  event.dataTransfer.getData('Text'))"
  style="ime-mode:Disabled"
><br>

大写英文:<xmp style= "display:inline"> </xmp>
<input onkeypress = "return regInput(this, /^[A-Z]*$/,  String.fromCharCode(event.keyCode))"
  onpaste  = "return regInput(this, /^[A-Z]*$/,  window.clipboardData.getData('Text'))"
  ondrop  = "return regInput(this, /^[A-Z]*$/,  event.dataTransfer.getData('Text'))"
  style="ime-mode:Disabled">
<br>

任意数字:<xmp style="display:inline"> </xmp>
<input onkeypress = "return regInput(this, /^[0-9]*$/,  String.fromCharCode(event.keyCode))"
  onpaste  = "return regInput(this, /^[0-9]*$/,  window.clipboardData.getData('Text'))"
  ondrop  = "return regInput(this, /^[0-9]*$/,  event.dataTransfer.getData('Text'))"
  style="ime-mode:Disabled"
><br>

限2位小数:<xmp style="display:inline"> </xmp>
<input onkeypress = "return regInput(this, /^\d*\.?\d{0,2}$/,  String.fromCharCode(event.keyCode))"
  onpaste  = "return regInput(this, /^\d*\.?\d{0,2}$/,  window.clipboardData.getData('Text'))"
  ondrop  = "return regInput(this, /^\d*\.?\d{0,2}$/,  event.dataTransfer.getData('Text'))"
  style="ime-mode:Disabled"
> 如: 123.12<br>


日   期:<xmp style="display:inline"> </xmp>
<input onkeypress = "return regInput(this, /^\d{1,4}([-\/](\d{1,2}([-\/](\d{1,2})?)?)?)?$/,  String.fromCharCode(event.keyCode))"
  onpaste  = "return regInput(this, /^\d{1,4}([-\/](\d{1,2}([-\/](\d{1,2})?)?)?)?$/,  window.clipboardData.getData('Text'))"
  ondrop  = "return regInput(this, /^\d{1,4}([-\/](\d{1,2}([-\/](\d{1,2})?)?)?)?$/,  event.dataTransfer.getData('Text'))"
  style="ime-mode:Disabled"
> 如: 2002-9-29<br>

任意中文:<xmp style="display:inline"> </xmp>
<input onkeypress = "return regInput(this, /^$/,     String.fromCharCode(event.keyCode))"
  onpaste  = "return regInput(this, /^[\u4E00-\u9FA5]*$/, window.clipboardData.getData('Text'))"
  ondrop  = "return regInput(this, /^[\u4E00-\u9FA5]*$/, event.dataTransfer.getData('Text'))"
><br>

部分英文:<xmp style="display:inline"> </xmp>
<input onkeypress = "return regInput(this, /^[a-e]*$/,  String.fromCharCode(event.keyCode))"
  onpaste  = "return regInput(this, /^[a-e]*$/,  window.clipboardData.getData('Text'))"
  ondrop  = "return regInput(this, /^[a-e]*$/,  event.dataTransfer.getData('Text'))"
  style="ime-mode:Disabled"
> 范围: a,b,c,d,e<br>

部分中文:<xmp style="display:inline"> </xmp>

<script language=javascript>
function checkChinese(oldLength, obj)
{
 var oTR = window.document.selection.createRange()
 var reg = /[^一二三四五六七八九十]/g
 oTR.moveStart("character", -1*(obj.value.length-oldLength))
 oTR.text = oTR.text.replace(reg, "")
}
</script>
<input onkeypress="return false" onkeydown="setTimeout('checkChinese('+this.value.length+','+this.uniqueID+')', 1)"
  onpaste  = "return regInput(this, /^[一二三四五六七八九十]*$/,  window.clipboardData.getData('Text'))"
  ondrop  = "return regInput(this, /^[一二三四五六七八九十]*$/,  event.dataTransfer.getData('Text'))"
> 范围: 一二三四五六七八九十<br>
</body>
</html>

相关阅读
javascript表格内移动上下列,可以做成ajax加上动态程序做成上下移动顺序
ASP事务处理 for access
盐城市开心假日旅行社有限公司
利用window.open函数控制弹出窗口,设置个性的弹出窗口
呼伦贝尔市北天草原旅行社有限公司
另外的一种可以计算的倒计时效果
Javascript实现CSS代码高亮显示
购物时某件商品倒计时销售非常不错
更多信息>>栏目类别选择
百度小程序开发
微信小程序开发
微信公众号开发
uni-app
asp函数库
ASP
DIV+CSS
HTML
python
更多>>同类信息
dw里面查找替换使用正则删除sqlserver里面的CONSTRAINT
javascript获取浏览器指纹可以用来做投票
火狐Mozilla Firefox出现:无法载入您的Firefox配置文件 它可能已经丢失 或是无法访问 问题解决集合处理办法
DW设置之后更好用 DreamweaverCS编辑GB2312与UTF-8文件在代码视图中点击鼠标错位问题的解决办法
js指定日期加n天加n月加n年
jquery中动态生成的代码使用on hover事件时不出现效果
更多>>最新添加文章
dw里面查找替换使用正则删除sqlserver里面的CONSTRAINT
Android移动端自动化测试:使用UIAutomatorViewer与Selenium定位元素
抖音直播音挂载小雪花 懂车帝小程序
javascript获取浏览器指纹可以用来做投票
火狐Mozilla Firefox出现:无法载入您的Firefox配置文件 它可能已经丢失 或是无法访问 问题解决集合处理办法
在Android、iOS、Windows、MacOS中微信小程序的文件存放路径
python通过代码修改pip下载源让下载库飞起
python里面requests.post返回的res.text还有其它的吗
更多>>随机抽取信息
兼容ie6+和火狐的禁止右键
jquery获取不同类型表单值的方式
nyfreetour.com
javascript取汉字拼音首字母缩写程序
温州远方国际旅行社
CSS的优化与技巧