想法是:移除非法号码,只允许11位数字号码,移除重复,然后使用指定的号码段进行区别运营商再确定的条数与分隔符号来分组;
最后方便复制与标志用过条
------------代码------------
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gbk"/>
<title>11位中国手机号码整理程序</title>
<style>
.box100{width:100%;border:1px solid black;}
.input{border:1px solid black;}
legend{font-weight:bold;}
body{font-size:12px;}
input,textarea{font-size:12px;}
#illegalDiv, #repeatDiv{color:red;}
#illegalDiv span, #repeatDiv span{color:blue;}
pre{display:inline;}
li{list-style-type:decimal;}
</style>
</head>
<body>
<input type="button" value="开始进行格式化处理" id="doBt" /> <span style="color:red;">点击开始后,请不要中途修改内容,直到提示完成.</span><br/>
<br/>
<fieldset>
<legend>移动</legend>
号码段(11位手机号码中前3位;用','号分隔)<br/>
<input class="box100" id="num3YD" value="134,135,136,137,138,139,147,150,151,152,157,158,159,182,183,184,187,188" />
<br />
<input class="input" value="10" id="groupNumYD" size="3" maxlength="3"/>个号码为一组
<br/>
组中号码之间用<input class="input" value=";" id="groupSplitYD" size="1" maxlength="1"/>分隔
</fieldset>
<br />
<fieldset>
<legend>联通整理</legend>
号码段(11位手机号码中前3位;用','号分隔)<br/>
<input class="box100" id="num3LT" value="130,131,132,145,155,156,185,186" />
<br />
<input class="input" value="10" id="groupNumLT" size="3" maxlength="3"/>个号码为一组
<br/>
组中号码之间用<input class="input" value=";" id="groupSplitLT" size="1" maxlength="1"/>分隔
</fieldset>
<br />
<fieldset>
<legend>电信</legend>
号码段(11位手机号码中前3位;用','号分隔)<br/>
<input class="box100" id="num3DX" value="133,153,180,181,189" />
<br/>
<input class="input" value="10" id="groupNumDX" size="3" maxlength="3"/>个号码为一组
<br/>
组中号码之间用<input class="input" value=";" id="groupSplitDX" size="1" maxlength="1"/>分隔
</fieldset>
<br />
待整理号码(请只输入数字,其它字符都在格式化时被视为分隔符号或删除或合并,最后只保留数字号码进行整理)
<br />
<textarea title="输入的号码必须是正确有效格式的11位中国手机号码,二个号码分隔字符可以是换行或是空格(space/tab),无效号码将在整理中忽略,出现在非法号码框中." class="box100" style="height:100px;" id="todoInput">
</textarea>
<br />
整理后号码
<br/>
<div id="tidyDiv" class="box100"></div>
<br/>
非法号码
<br/>
<div id="illegalDiv" class="box100"></div>
<br/>
重复号码
<br/>
<div id="repeatDiv" class="box100"></div>
<script>
var numbers;
function _(id) {
if (id.substr(0,1) == '&') return document.getElementsByName(id.substr(1));
return document.getElementById(id);
}
_('doBt').onclick = function () {
bodyRW(0);
if (!ckCFG()) {
bodyRW(1);
return;
}
if (!format()) {
bodyRW(1);
return alert('剔除非数字内容,无需要整理号码');
}
if (!splitIllegal()) {
bodyRW(1);
return alert('剔除无效号码后,无需要整理号码');
}
groupShow();
bodyRW(1);
}
function bodyRW (rw) {
rw ? document.body.removeAttribute('readonly') : document.body.setAttribute('readonly', 'readonly');
}
function ckCFG() {//检测配置信息
if (!/^1[3458]\d(?:,1[3458]\d)*$/.test(_('num3LT').value)) {
alert('联通号码段非法,号码之间分隔符必须是英文逗号;3位号码必须是1[3458]X格式;');
_('num3LT').focus();
return 0;
}
if (!(/^\d+$/.test(_('groupNumLT').value) && (_('groupNumLT').value > 0))) {
alert('联通一组号码个数值有误');
_('groupNumLT').focus();
return 0;
}
if (/^\s*$/g.test(_('groupSplitLT').value)) {
alert('联通一组号码分隔符未填写');
_('groupSplitLT').focus();
return 0;
}
if (!/^1[3458]\d(?:,1[3458]\d)*$/.test(_('num3YD').value)) {
alert('移动号码段非法,号码之间分隔符必须是英文逗号;3位号码必须是1[3458]X格式;');
_('num3YD').focus();
return 0;
}
if (!(/^\d+$/.test(_('groupNumYD').value) && (_('groupNumYD').value > 0))) {
alert('移动一组号码个数值有误');
_('groupNumYD').focus();
return 0;
}
if (/^\s*$/g.test(_('groupSplitYD').value)) {
alert('移动一组号码分隔符未填写');
_('groupSplitYD').focus();
return 0;
}
if (!/^1[3458]\d(?:,1[3458]\d)*$/.test(_('num3DX').value)) {
alert('电信号码段非法,号码之间分隔符必须是英文逗号;3位号码必须是1[3458]X格式;');
_('num3DX').focus();
return 0;
}
if (!(/^\d+$/.test(_('groupNumDX').value) && (_('groupNumDX').value > 0))) {
alert('电信一组号码个数值有误');
_('groupNumDX').focus();
return 0;
}
if (/^\s*$/g.test(_('groupSplitDX').value)) {
alert('电信一组号码分隔符未填写');
_('groupSplitDX').focus();
return 0;
}
if (/^\s*$/g.test(_('todoInput').value)) {
alert('请先在待整理号码框中输入号码');
_('todoInput').focus();
return 0;
}
return 1;
}
function format() {//格式化号码
numbers = _('todoInput').value;
numbers = numbers.replace(/\D+/g, '\n').toString();
numbers = _('todoInput').value = numbers.replace(/^\s+|\s+$/g, '').toString();
return /^\s*$/g.test(numbers) ? 0 : 1;
}
function splitIllegal() {//剔除非法号码
numbers = numbers.split('\n');
var tmp = {lt:[], yd:[], dx:[]};
var illHtml = '';
var repeat = [];
var num3sLT = ',' + _('num3LT').value + ',';
var num3sYD = ',' + _('num3YD').value + ',';
var num3sDX = ',' + _('num3DX').value + ',';
var exist = {};
for (var i = 0; i < numbers.length; i++) {
var n = numbers[i];
if (n.length < 11) {
illHtml += '<div><span>' + numbers[i] + '</span>:位数不足11位</div>';
continue;
} else if (13 == n.length) {
if (n.indexOf(86) == 0) n = n.substr(2);
else {
illHtml += '<div><span>' + numbers[i] + '</span>:中国13位手机号码必须是以86开头</div>';
continue;
}
} else if (11 != n.length) {
illHtml += '<div><span>' + numbers[i] + '</span>:号码个数非法</div>';
continue;
}
var num3 = n.substr(0, 3);
var who;
if (num3sLT.indexOf(',' + num3 + ',') > -1) {//联通号码
who = 'lt';
} else if (num3sYD.indexOf(',' + num3 + ',') > -1) {//移动号码
who = 'yd';
} else if (num3sDX.indexOf(',' + num3 + ',') > -1) {//电信号码
who = 'dx';
} else {
illHtml += '<div><span>' + n + '</span>:前面3位号码段"' + num3 + '"不存在:移动/联通/电信</div>';
continue;//必须,防止下走
}
if (exist[n]) {
repeat[repeat.length] = n;
continue;//必须,防止下走
}
exist[n] = 1;
tmp[who][tmp[who].length] = n;
}
_('illegalDiv').innerHTML = illHtml;
_('repeatDiv').innerHTML = repeat.sort(sortInt).join('<br />');
tmp.lt && tmp.lt.sort(sortInt);
tmp.yd && tmp.yd.sort(sortInt);
tmp.dx && tmp.dx.sort(sortInt);
numbers = tmp;
return tmp.lt.length || tmp.yd.length || tmp.dx.length;
}
function sortInt(a, b) {
if (a < b) return -1;
return a == b ? 1 : 0;
}
function groupShow() {//按格式显示
var html = '<fieldset><legend>移动组 <a href="#1" οnclick="cpAll(\'yd\', this);">全部复制</a></legend>';
html += cutNumber('yd', _('groupNumYD').value, _('groupSplitYD').value);
html += '</fieldset><br /><fieldset><legend>联通组 <a href="#1" οnclick="cpAll(\'lt\', this);">全部复制</a></legend>';
html += cutNumber('lt', _('groupNumLT').value, _('groupSplitLT').value);
html += '</fieldset><br /><fieldset><legend>电信组 <a href="#1" οnclick="cpAll(\'dx\', this);">全部复制</a></legend>';
html += cutNumber('dx', _('groupNumDX').value, _('groupSplitDX').value);
html += '</fieldset>';
_('tidyDiv').innerHTML = html;
}
function cutNumber(who, gl, spl) {//组织组
var w = who;
who = numbers[who];
var html = '';
var i = 0;
gl = gl < 2 ? 1 : gl * 1;
var li = 0;
while (i < who.length) {
var l = who.slice(i, i + gl).join(spl);
html += '<li><input type="checkbox" id="' + w + 'A' + li + 'Check" name="' + w + 'ACheck"/><a href="#1" name="' + w + 'A" id="' + w + 'A' + li + '" title="点我一下即可完成复制我" οnclick="cp(this.innerText, this);_(this.id + \'Check\').checked = \'checked\';">';
html += l;
i += gl;
html += '</a></li>';
li++;
}
return html == '' ? '' : '<ul>' + html + '</ul>';
}
function cpAll(who, obj) {//复制整组
var w = who;
who = _('&' + w + 'A');
var checks = _('&' + w + 'ACheck');
var txt = '';
for (var i = 0; i < who.length; i++) {
txt += who[i].innerText + '\n';
flash(who[i]);
checks[i].checked = 'checked';
}
cp(txt, obj);
}
function cp(txt, obj) {//复制
window.clipboardData.clearData();
window.clipboardData.setData("Text", txt);
flash(obj);
}
function flash(obj) {//闪动
if (obj) {
var clBak = obj.style.color
obj.style.color = 'white';
setTimeout(function(){obj.style.color = 'red';}, 200);
setTimeout(function(){obj.style.color = clBak;}, 400);
}
}
</script>
</body>
</html>