function checkBlkmdn(form)
{
    var blkmdn = Trim(form.blackMdn.value);
    //检查是否含有非法字符
    if( !(/^[0-9\s]+$/.test(blkmdn)) )
    {
        alert("终端号码只能数字");
        return false;
    }
    //去掉首尾的","
    var temp = blkmdn.replace(/\r\n/ig,",");
    var temp = temp.replace(/\b/g,",");
    var pos_tmp = temp.indexOf(",");
    while (pos_tmp==0 && temp.length > 0)
    {
        temp = temp.substr(1,temp.length)
        pos_tmp = temp.indexOf(",");
    }
    pos_tmp = temp.lastIndexOf(",");
    while ( pos_tmp == temp.length-1 && temp.length > 0)
    {
        temp = temp.substr(0,pos_tmp)
        pos_tmp = temp.lastIndexOf(",");
    }
    mdnChanged = temp;
    var pos = temp.indexOf(",");
    while (pos>=0)
    {
        mdn = Trim(temp.substr(0,pos));
        temp = temp.substr(pos+1,temp.length);
        if(mdn.length > 0)
        {
            if(!(/^\d+$/.test(Trim(mdn))))
            {
                alert("每个终端号码只能为位数字!");
                form.blackMdn.focus();
                return false
            }
             
        }
       
    pos = temp.indexOf(",");
    }
    if (pos<0)
    {
       mdn = temp
       if(!(/^\d+$/.test(Trim(mdn))) )
            {
                alert("每个终端号码只能为数字!");
                form.blackMdn.focus();
                return false
            }                }
    return true;
}
这是一个验证界面输入的多个用空格或回车逗号分开的带区号的号码的验证脚本,我现在想得到循环出来的mdn号码的数量,怎么改啊

解决方案 »

  1.   

    这段代码怎么又用正则,又用字符串操作,感觉有些乱
    按照这个思路改的话,代码没测试,思路差不多这样
    var pos_tmp = temp.indexOf(",");
    这句下面加
    var pos_temp=temp.match(/,/g)
    if(pos_temp)
    pos_count=pos_temp.length+parseInt(1)
    else
    {
       pos_count=(temp.length==0)?0:1
    }
    alert(pos_count)//这个就是总数