輸入手機號自動分隔
阿新 • • 發佈:2017-10-27
regex his tel val horizon turn trim margin slice
<!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>
<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>
輸入手機號自動分隔