日期:2014-05-18  浏览次数:20661 次

怎么控制文本域里只能让用户输入5个手机号
var targetArray = null;
var targetStr;
targetStr = document.all("targetHMS").value;

if(targetStr.length == 0 || targetStr == ""){
alert("请输入电话号码!");
return false;
}else{
targetStr = replaceAll(targetStr,",", ",");
targetArray = targetStr.split(",");

var targetFlag = false;
if(targetArray.length > 5)
{
alert("您输入的号码超过了5个,请检查!");
return false;
}else{
for(var i = 0; i < targetArray.length; i++){
for(var j = 0; j < targetArray[i].length; j++){
if(targetArray[i].charAt(j) < '0' || targetArray[i].charAt(j) > '9'){
alert("您输入的电话号码有误,请检查!");
return false;
}
}

if(targetArray[i].length < 8 || targetArray[i].length > 12){
alert("您输入的电话号码有误,请检查!");
return false;
}else if(targetArray[i].length == 9 || targetArray[i].length == 10){
alert("您输入的电话号码有误,请检查!");
return false;


还必须是131,132,133,的手机号呀

------解决方案--------------------
试试用正则:(131|132|133)[0-9]{8}
------解决方案--------------------
var numSub = new Array();
numSub[1]=131;
numSub[2]=132;
numSub[3]=133;

function moNumber(targetStr){
 if(targetStr.length == 0 || targetStr == ""){
alert("请输入电话号码!"); 
return false;
 }else{
if(targetStr.indexOf(",")==-1){
alert("电话号码请用逗号隔开!"); 
return false;
}else{
var numArr=targetStr.split(","); 
if(numArr.length > 5 /**||numArr.length ==0*/)
{
alert("您输入的号码超过了5个,请检查!");
return false;
}else{

for(var i=0;i< numArr.length;i++){
var flag=0;
for(var j=0;j<numSub.length();i++){
if(numArr.subString(0,2)!=numSub[j]){
flag=1;
}else{
flag=0;
}
}if(flag==1){alert("手机号码必须是131,132,133开头的");break;}
}
}

}
}

}

------解决方案--------------------
HTML code
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=gbk" />
  <title>试试</title>
</head>

<script type="text/javascript">
function checkMobile() {
  var m = document.getElementById('mobile');
  if(!m) {
    return false;
  }
  var regex = /^13[1-3][0-9]{8}(?:\s*,\s*13[1-3][0-9]{8}){4}$/;
  if(regex.test(m.value)) {
    return true;
  } else {
    alert('请输入 5 个以 131, 132, 133 开头的手机号,以“,”分隔');
    m.focus();
    m.select();
    return false;
  }
}
</script>

<style type="text/css">
input#mobile {
  width: 500px;
}
</style>
<body>
<form name="frm" method="post" action="#" onsubmit="return checkMobile();">
  手机号:<input type="text" name="mobile" id="mobile"><br/>
  <input type="submit" value="确定">
</form>
</body>
</html>