首页 > 移动平台 > 详细

输入手机号自动分隔

时间:2017-10-27 14:38:44      阅读:219      评论:0      收藏:0      [点我收藏+]
<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<title></title>
</head>

<body>
<form class="form-horizontal" style="margin-top: 100px;">
<div class="form-group">
<label>PHONE</label>
<input type="tel" class="userPhone" onkeyup="this.blur();this.focus();" maxlength="13" id="" placeholder="phone">
</div>
</form>

<script type="text/javascript">
var inputs = document.querySelector(‘.userPhone‘);
var oninput = onchange || oninput;
inputs.oninput = function() {
var str = inputs.value;
str = formatNum(str.replace(/\D/g, ‘‘), true);
inputs.value = str;
}

function formatNum(num, isTel, count) {
isTel = isTel || 0;
count = count || 4;

if (isTel) {
num = ‘0‘ + num;
}

var patt = new RegExp(‘\\d{‘ + count + ‘}‘, ‘g‘)
num = num.replace(patt, function($0) {
return $0 + ‘ ‘;
})

if (isTel) {
num = num.slice(1).trim()
}
return num;
}
</script>
</body>

</html>

输入手机号自动分隔

原文:http://www.cnblogs.com/cisum/p/7742842.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!