文本框中禁止非数字字符输入比如手机号码、邮编

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

文本框中禁止非数字字符输入比如手机号码、邮编

  2020-05-13 我要评论
在工作中,总是遇到很多禁止非数字字符输入的文本框,比如手机号码了 邮编了
复制代码 代码如下:

<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="style.css">
</head>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<body>
<input type="text" id="phone" value="0000">
</body>
<script type="text/javascript">
jQuery(document).ready(function($) {
// Stuff to do as soon as the DOM is ready;
var phone=$('#phone');
$(phone).on('click',function(){
phone.val('');
})
$(phone).on('keyup',function(evt){
var phoneVal=phone.val();
phoneVal=phoneVal.replace('/[^\d]+/g', ''); //替换非数字字符为空格
phoneVal=parseInt(phoneVal,10);
if(isNaN(phoneVal)){
phoneVal = '';
}
this.value=phoneVal;
})
});
</script>
</html>

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们