2013-08-01 213 views
0

之間我只想來驗證IP地址只接受3三個點的一些數字適當點的IP地址驗證在

除權後: 有效:191.123.121.202是有一定的小數點後3個點有效。 無效:191..123.121.202無效,其中2點在序列

整點:想要一個強大的IP驗證

$("input.onlynumberdecimal").keydown(function (event) { 

     console.log(event.keyCode); 

     if (event.shiftKey == true) { 
      event.preventDefault(); 
     } 

     if ((event.keyCode >= 48 && event.keyCode <= 57) || 
      (event.keyCode >= 96 && event.keyCode <= 105) || 
      event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 37 || 
      event.keyCode == 39 || event.keyCode == 46 || event.keyCode == 190) { 

     } else { 
      event.preventDefault(); 
     } 

     if($(this).val().indexOf('.') !== -1 && event.keyCode == 190) 
      event.preventDefault(); 
     //if a decimal has been added, disable the "."-button 

    }); 

在一定程度上我有一些其他網站的幫助了。如果用戶複製並粘貼正確的IP,那麼它也應該接受,否則它不應該允許他粘貼。

DEMO

+0

[改變IP地址輸入框]的可能重複(HTTP:/ /stackoverflow.com/questions/12304612/input-box-for-changing-ip-address) –

+1

對blur()而不是keypress/down()進行驗證更爲典型。一次驗證整個文本要容易得多,邊緣案例要少得多(例如複製/粘貼的例子)。 – Phylogenesis

+1

甚至更​​好的提交,然後再在服務器上 – mplungjan

回答

7

DEMO

試試這個

var pattern = /\b(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]?)\b/; 
x = 46; 
$('input[type="text"]').keypress(function (e) { 
    if (e.which != 8 && e.which != 0 && e.which != x && (e.which < 48 || e.which > 57)) { 
     console.log(e.which); 
     return false; 
    } 
}).keyup(function() { 
    var this1 = $(this); 
    if (!pattern.test(this1.val())) { 
     $('#validate_ip').text('Not Valid IP'); 
     while (this1.val().indexOf("..") !== -1) { 
      this1.val(this1.val().replace('..', '.')); 
     } 
     x = 46; 
    } else { 
     x = 0; 
     var lastChar = this1.val().substr(this1.val().length - 1); 
     if (lastChar == '.') { 
      this1.val(this1.val().slice(0, -1)); 
     } 
     var ip = this1.val().split('.'); 
     if (ip.length == 4) { 
      $('#validate_ip').text('Valid IP'); 
     } 
    } 
}); 

更新與端口號的驗證IP地址。

Ex。 192.168.2.100:27896

var pattern = /\b(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]?)\:([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])\b/; 
x = 46; 
$('input[type="text"]').keypress(function (e) { 
    console.log(e.which); 
    if (e.which != 8 && e.which != 0 && e.which != x && e.which !=58 && (e.which < 48 || e.which > 57)) { 
     console.log(e.which); 
     return false; 
    } 
}).keyup(function() { 
    var this1 = $(this); 
    if (!pattern.test(this1.val())) { 
     $('#validate_ip').text('Not Valid IP'); 
     while (this1.val().indexOf("..") !== -1) { 
      this1.val(this1.val().replace('..', '.')); 
     } 
     x = 46; 
    } else { 
     x = 0; 
     var lastChar = this1.val().substr(this1.val().length - 1); 
     if (lastChar == '.') { 
      this1.val(this1.val().slice(0, -1)); 
     } 
     var ip = this1.val().split('.'); 
     if (ip.length == 4) { 
      $('#validate_ip').text('Valid IP'); 
     } 
    } 
}); 

WORKING FIDDLE

+0

爲什麼'\ b'而不是'^'或'$'? –

+0

不要忘記'10..1'是一個有效的IP地址 –

+0

@GeraldSchneider是的,你可以檢查你自己http://jsfiddle.net/cse_tushar/aPM5X/2 –

1
function isIpAddress(s) { 
    if (typeof s !== 'string') { return false; } 
    // There must be 4 parts separated by dots. 
    var parts = s.split('.'); 
    if (parts.length !== 4) { return false; } 
    // Each of the four parts must be an integer in the range 0 to 255. 
    for (var i = 0; i < 4; ++i) { 
    var part = parts[i]; 
    // Each part must consist of 1 to 3 decimal digits. 
    if (!/^\d{1,3}$/.test(part)) { return false; } 
    var n = +part; 
    if (0 > n || n > 0xff) { return false; } 
    } 
    return true; 
} 
0

試試這個。

<SCRIPT language="JavaScript"> 
function verifydata(incoming) 
{ 
    errorlog = "" 

    // CHECK ALL THE FIELDS TO VERIFY THEIR EXISTENCE 
    if (incoming.ipstart.value == "") 
     errorlog += "Starting IP Address is blank.\n" 
    if (incoming.ipend.value == "") 
     errorlog += "Ending IP Address is blank.\n" 
    if (!(/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(incoming.ipstart.value))) 
     errorlog += "Incorrect Starting IP Address Format.\n" 
    if (!(/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(incoming.ipend.value))) 
     errorlog += "Incorrect Ending IP Address Format.\n" 

} 
</SCRIPT> 
0

OR 可以使用jQuery Mask Plugin。使用起來非常簡單,它會添加。 (點)和數字佔位符將被標記。

0

以下解決方案將驗證輸入的IP地址值,包括格式&它也是值。它將接受分0.0.0.0和最大255.255.255.255。如果輸入的IP地址無效,則代碼將突出顯示粉紅色的輸入控件&清除文本,否則將保持輸入的有效值。

把輸入標籤頁上:在適當的Java腳本文件

<input type="text" name="IpAddress" id="IpAddress" class="input-medium" onkeypress="return IPAddressKeyOnly(event)" onblur="confirmIPAddress();"/> 

添加下面的方法或包括在網頁它的自我。

1)來處理按鍵事件:

function IPAddressKeyOnly(e) { 
    var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode; 
    if (keyCode != 46 && keyCode > 31 && (keyCode < 48 || keyCode > 57)) 
     return false; 
    return true; 
} 

2)處理正確的IP地址值驗證:

function confirmIPAddress() { 
    var ip = document.getElementById("IpAddress"); 
    if (ip.value.length >0 && ip.value.length<=15) { 
     ip.style.background = "white"; 
     var ipSlot=ip.value.split("."); 
     if(ipSlot.length==4){ 
     for (var i=0;i<ipSlot.length;i++){ 
     var l=ipSlot[i].length; 
      if (l >0 && l<=3){ 
       if(ipSlot[i]>=0 && ipSlot[i]<256){} 
       else{ip.value = "";ip.style.background = "pink";break ;return false;} 
      }else{ 
       ip.value = "";ip.style.background = "pink";break ;return false; 
      } 
     } 
    }else{ip.value = "";ip.style.background = "pink";return false; } 
} 
else{ip.value = "";ip.style.background = "pink";} 

}

0

試一次。

//Check Format 
var ip = ip.split("."); 

if (ip.length != 4) { 
    return false; 
} 

//Check Numbers 
for (var c = 0; c < 4; c++) { 
    //Perform Test 
    if(isNaN(parseFloat(ip[c])) || !isFinite(ip[c]) || ip[c] < 0 || ip[c] > 255 || ip[c].indexOf(" ") !== -1 || ip[c].match(/^-\d+$/)){ 

     return false; 
    } 
} 
return true; 
0
 //invalid ip send 
     ipvalidation('256.0.0.0'); 

     //ip validation function 
     function ipvalidation(x){ 
      limit = 255; 
      [ii, xx, yy, cc] = x.split('.'); 
      if(ii <= limit && xx <= limit && yy <= limit && cc <= limit){ 
       alert('valid ip'); 
      } else { 
       alert('invalid ip'); 
      } 
     } 
+1

請解釋你的代碼。 – Fidel90

1

使用正則表達式,這和正則表達式的IPv4地址是

/((25 [0-5] | 2 [0-4] [0-9] | [01 ] [0-9] [0-9])(| $)){4}/

function validateIP(ipAddress){ ipv4Re = new RegExp('^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}', 'i'); if(ipv4Re.test(ipAddress)){ return "its a vaild address"} else return "its an invalid address"} 

它會驗證:?。

  • 0。 0.0.0
  • 255.255.255.255
  • 191.123.121.202

作廢: - 191..123.121.202