您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

javascript怎样设置文本框不能输入数字

2025/3/30 16:11:47发布78次查看
在javascript中,可以利用if语句和keycode来设置文本框不能输入数字,0到9的keycode值为48到57,只要控制文本框内的keycode值不在该范围内,语法为“if(charcode>46&&charcode<58)”。
本教程操作环境:windows10系统、javascript1.8.5版、dell g3电脑。
javascript怎样设置文本框不能输入数字
数字0-9keycode的值为48-57,只要在javascript中设置文本框中输入keycode的值在这个范围内就就取消此行为,以此就可以实现文本框不能输入数字。
示例如下:
或者:
<!doctype html><html lang="en"><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <title>document</title></head><body><input type="text" id="test"> <script type="text/javascript"> window.onload=function(){ document.getelementbyid('test').addeventlistener('keypress',function(e){ var charcode=e.charcode; if(charcode>46&&charcode<58) /*0-9 的charcode*/ e.preventdefault(); }); } </script></body></html>
输出结果与上述结果相同,文本框内无法输入数字。
【相关推荐:javascript学习教程】
以上就是javascript怎样设置文本框不能输入数字的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product