2014-04-02 210 views
0

我必須驗證從表單提交的IP地址。我已經在jQuery中嘗試了下面的代碼,但它不起作用。使用jQuery手機驗證ip地址

var IpAddresslanText = $("#IpAddresslan").val(); 
if(IpAddresslanText == '') { 
    alert('enter IpAddresslan'); 
    return false; 
} 
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]?)$/; 
if(!IpAddresslanText.match(ipformat)) { 
    alert("You have entered an invalid IP address!"); 
    return false; 
} 

在此先感謝

+0

http://stackoverflow.com/questions/17587994/how-to-check-validation-of-ip-address-in-jquery –

回答

0

試試這個

jQuery.validator.addMethod('validIP', function(value) { 
    var split = value.split('.'); 
    if (split.length != 4) 
    return false; 

    for (var i=0; i<split.length; i++) { 
    var s = split[i]; 
    if (s.length==0 || isNaN(s) || s<0 || s>255) 
     return false; 
    } 
    return true; 
}, ' Invalid IP Address'); 

用法:

jQuery("#myForm").validate({ 
    rules: { 
    name: { 
     validIP: true 
    } 
    } 
} 
0

match返回數組。

所以試圖改變自己的狀況是這樣的:

if(IpAddresslanText.match(ipformat)===null)