文本框中禁止非数字字符输入比如手机号码、邮编
在工作中,总是遇到很多禁止非数字字符输入的文本框,比如手机号码了 邮编了
代码如下:
<!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> 相关推荐
lupeng 2020-11-14
sjcheck 2020-11-10
meylovezn 2020-08-28
owhile 2020-08-18
Francismingren 2020-08-17
pythonclass 2020-07-29
sunzhihaofuture 2020-07-19
爱读书的旅行者 2020-07-07
行吟阁 2020-07-05
tianqi 2020-07-05
行吟阁 2020-07-04
冰蝶 2020-07-04
lyg0 2020-07-04
owhile 2020-07-04
opspider 2020-06-28
lengyu0 2020-06-28
tianqi 2020-06-21
dadaooxx 2020-06-16