html代码:
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
#content #contentmain table tbody tr td {
border:0;
height:28px;
}
#content #contentmain table{
text-align: left;
width:100%;
}
.tabel_2{padding:10px;}
.coninf table td{line-height: 30px;}
.coninf table{width: 100%;line-height: 25px;border-collapse: collapse;}
.wxw-table{margin-bottom: 10px;}
.wxw-table td{border: 1px solid #c5c5c5;height: 28px;text-align: center;}
</style>
<script src="http://code.jquery.com/jquery-1.4.1.min.js"></script>
</head>
<body>
<div id="content">
<div id="contentmain">
<form>
<table class="tabel_2" >
<tbody>
<tr style="border-top:1px solid #ccc;">
<td align="right" valign="top" style="padding-top:10px;">手机号码:</td>
<td style="padding-top:10px;">
<textarea rows="3" cols="50" class="phones"></textarea>
</td>
</tr>
<tr>
<td></td>
<td class="phone_num">
共计号码:<span style="color:red">0</span>个
</td>
</tr>
<tr>
<td></td>
<td >
<input type="button" name="" value="过滤错号" class="querybtn" id="wrongNumber">
<input type="button" name="" value="重号过滤" class="querybtn" id="repeatNumber">
<input type="button" name="" value="二次过滤" class="querybtn" id="doubleNumber">
</td>
</tr>
</tbody>
</table>
</form>
</div>
</div>
</body>
</html>
界面如下:
js代码:
<script type="text/javascript">
//计算号码
var $phone = $(".phones");
var ie = jQuery.support.htmlSerialize;
if(ie){
$phone[0].oninput = calculatePhone;
}else{
$phone[0].onpropertychange = calculatePhone;
}
function calculatePhone(){
if($(".phones").val().indexOf(",")>=0){
var strAry = $(".phones").val().split(",") ;
}else{
var strAry = $(".phones").val().split("\n");
};
$(".phone_num").html("共计号码:<span style=‘color:red‘>"+strAry.length+"</span>个");
}
//过滤错号
$("#wrongNumber").click(function(){
if($(".phones").val().indexOf(",")>=0){
var strAry = $(".phones").val().split(",") ;
}else{
var strAry = $(".phones").val().split("\n");
};
wrong_num(strAry)
})
//重号过滤
$("#repeatNumber").click(function(){
if($(".phones").val().indexOf(",")>=0){
var strAry = $(".phones").val().split(",") ;
}else{
var strAry = $(".phones").val().split("\n");
};
uniQueue(strAry);
});
//二次过滤
$("#doubleNumber").click(function(){
if($(".phones").val().indexOf(",")>=0){
var strAry = $(".phones").val().split(",") ;
}else{
var strAry = $(".phones").val().split("\n");
};
var arr = uniQueue(strAry);
wrong_num(arr)
})
//重号过滤函数
function uniQueue(array){
var arr=[];
var m;
while(array.length>0){
m=array[0];
arr.push(m);
array=$.grep(array,function(n,i){
return n==m;
},true);
}
$(".phones").val(arr);
$(".phone_num").html("共计号码:<span style=‘color:red‘>"+arr.length+"</span>个");
return arr;
}
//错误号码过滤函数
function wrong_num(strAry){
var isMobile=/^(?:13\d|15\d|18\d)\d{5}(\d{3}|\*{3})$/;
var wrong_num=[];
for(var i=0;i<strAry.length;i++){
RegCellPhone = /^([1][0-9]{10})?$/;
falg=strAry[i].search(RegCellPhone);
if (falg==-1){
wrong_num.push(i);
}
}
for(var j=0;j<wrong_num.length;j++){
strAry.splice(wrong_num[i],1);
}
$(".phones").val(strAry);
$(".phone_num").html("共计号码:<span style=‘color:red‘>"+strAry.length+"</span>个");
}
</script>