检查'undefined'&& javascript中的ip格式无效

时间:2017-04-11 06:21:21

标签: javascript html match nan getelementsbyclassname

我想检查一下ip地址列表..

'null'value和正确的ip格式......

HTML的一部分

<form name="save" method="post" onsubmit="return validateForm()" action="confresult.php" target="resultIframe">

          <tr id="ipcnt"><td colspan="2">No. of IP</td>
        <td><select name="ipcnt" size="1" onChange="switch();">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
          </select></td></tr>
<tr id="mmeip[0]"><td colspan="2">IP1</td>
<td><input type="text" class="mmeIp" name="remoteip[0]" size="20" maxlength="15" value="10.1.35.31"></td></tr>
<tr id="mmeip[1]"><td colspan="2">IP2</td>
<td><input type="text" class="mmeIp" name="remoteip[1]" size="20" maxlength="15" value="10.1.35.32"></td></tr>
<tr id="mmeip[2]"><td colspan="2">IP3</td>
<td><input type="text" class="mmeIp" name="remoteip[2]" size="20" maxlength="15" value=""></td></tr>
<tr id="mmeip[3]"><td colspan="2">IP4</td>
<td><input type="text" class="mmeIp" name="remoteip[3]" size="20" maxlength="15" value=""></td></tr>
<tr id="mmeip[4]"><td colspan="2">IP5</td>
<td><input type="text" class="mmeIp" name="remoteip[4]" size="20" maxlength="15" value=""></td></tr>
<tr id="mmeip[5]"><td colspan="2">IP6</td>
<td><input type="text" class="mmeIp" name="remoteip[5]" size="20" maxlength="15" value=""></td></tr>
<tr id="mmeip[6]"><td colspan="2">IP7</td>
<td><input type="text" class="mmeIp" name="remoteip[6]" size="20" maxlength="15" value=""></td></tr>
<tr id="mmeip[7]"><td colspan="2">IP8</td>
<td><input type="text" class="mmeIp" name="remoteip[7]" size="20" maxlength="15" value=""></td></tr>

和Javascript函数的一部分是......

  if(validateIPaddress()==false){
    return false;
  }
function validateIPaddress(){
   var ipformat = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
   var ip = document.getElementsByClassName("mmeIp");
   var cnt = document.save.ipcnt;
   for(var i = 0; i < cnt.value; i++) {
        if(ip[i].value != "") {
            if(ip[i].value.match(ipformat)){
                return true;
            } else{
                alert("Please enter the valid IP address format for the\n [IP] field. \n");
                ip[i].focus();
                return false;
            }
        } else{
            alert("Please fill out the [IP] field.");
            ip[i].focus();
            return false;
        }
    }
}

它仅适用于第一个IP(mmeIp [0])地址..我认为for()循环不起作用...

为什么只检查第一个ip ???

如何检查'ipcnt'ip ??

的数量

1 个答案:

答案 0 :(得分:1)

因为如果第一个没有通过验证,则会使用return语句中断您的函数。 return将打破函数的进一步执行并返回该值。