输入文本限制的问题?

wangbin_168 2005-11-01 10:40:00

我想在文本框内只能让用户输入数字型的内容(输入时验证),当用户欲输入字符型的内容时写不进去,该怎样做呀?

下面是我在西安人才网上查的代码(我只找到了一个文本的东西):
<input maxLength="50" size="20" name="tel"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'))">

期望大侠帮小弟解决,多谢!
...全文
86 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
八哥 2005-11-01
  • 打赏
  • 举报
回复
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<style>
#dv{
width:100;
height:15;
overflow-x:hidden;
overflow-y:hidden;
}
</style>
<table width=500 border="0" align="left" cellpadding="0" cellspacing="1" bgcolor="#000000" style="word-break:break-all; " id="Test">
<tr height="20" bgcolor=#ffffff><td width=100 >不限制</td><td width=100>小写转大写</td><td width=100 >大写转小写</td><td width=100 >只能为正整数</td><td width=100>负数和小数</td></tr>
<tr height="20" bgcolor=#ffffff><td ><div id="dv" contentEditable=true></td><td ><div id="dv" contentEditable=true><td ><div id="dv" contentEditable=true></td><td ><div id="dv" contentEditable=true></td><td><div id="dv" contentEditable=true></td></tr>
</table>

<script language="vbScript">
Private sub Test_onkeypress()
Dim thisObject,KeyAscii
Set thisObject = Window.event.srcElement
If thisObject.parentElement.tagName<>"TD" Then Exit Sub
KeyAscii=window.event.keyCode
Select Case thisObject.parentElement.cellIndex
Case 1
CheckKeyCode KeyAscii,thisObject.innerText,1,0,0
Case 2
CheckKeyCode KeyAscii,thisObject.innerText,2,0,0
Case 3
CheckKeyCode KeyAscii,thisObject.innerText,0,0,0
Case 4
CheckKeyCode KeyAscii,thisObject.innerText,0,1,1
End select
End Sub

'=========================================================================================================
Public Sub CheckKeyCode(KeyAscii,objCode,UppeLower,Minus,Point)
'UppeLower 为0不管,为1小写转大写,为2大写转小写
'文本框只能输入数字 Minus 是否允许-负号输入,Pointl是否允许小数点输入
If UppeLower= 1 Then
If KeyAscii >96 And KeyAscii<123 Then
KeyAscii=KeyAscii-32
End If
ElseIf UppeLower= 2 Then
If KeyAscii >64 And KeyAscii<91 Then
KeyAscii=KeyAscii+32
End If
ElseIf KeyAscii<58 And KeyAscii>44 And KeyAscii<>47 Then
If Minus And KeyAscii=45 And objCode="" Then
'无变化,可以输入负号
ElseIf KeyAscii=8 Then
'无变化,退格键不屏蔽
ElseIf KeyAscii=46 And Point And InStr(objCode, ".")=0 Then
'无变化,可以输入小数
ElseIf KeyAscii> 47 And KeyAscii< 58 THEN
Else
KeyAscii=0
End IF
ELSE
KeyAscii=0
End If
window.event.keyCode=KeyAscii
End Sub

</SCRIPT>
wangbin_168 2005-11-01
  • 打赏
  • 举报
回复
多谢了,现在结贴了!
jspadmin 2005-11-01
  • 打赏
  • 举报
回复
同意楼上写法,onkeyup过滤
----------------------------------------------------------------------------------
http://community.csdn.net/Expert/topic/4343/4343643.xml?temp=.6227838。
批量文件自动搜索替换插入器已经更新为V2.1 for CSDN friend,下载地址:http://www.pifoo.com/blog/user1/1/archives/2005/288.shtml

monkeyhjl 2005-11-01
  • 打赏
  • 举报
回复
<input type="text" name="num" onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))" size="3">

28,406

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧