在工作中,总是遇到很多禁止非数字字符输入的文本框,比如手机号码了 邮编了
复制代码 代码如下:
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="style.css">
</head>
<script src="/UploadFiles/2021-04-02/jquery.js"><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>
复制代码 代码如下:
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="style.css">
</head>
<script src="/UploadFiles/2021-04-02/jquery.js"><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>
标签:
文本框,非数字字符
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“文本框中禁止非数字字符输入比如手机号码、邮编”评论...