2017-09-14 45 views
0

我有了文本字段用戶輸入一個或多個文本字段,並繼續搜索,兩個文本字段是IP地址的形式:如何驗證兩個文本輸入,如果一個被填充,驗證應該忽略另一個?

IP地址 外部IP地址 我使用jQuery驗證,但是當用戶想要搜索僅匹配IP地址的結果時,外部IP地址的其他字段會顯示驗證錯誤,請僅顯示填充的IP字段的驗證錯誤? 以下是我在js文件:

jQuery.validator.addMethod("ipAddressFormat",function(value, element){ 
theName = "IPaddress"; 
var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/; 
var ipArray = value.match(ipPattern); 
if (value == "0.0.0.0" || value == "255.255.255.255" || 
    value == "10.1.0.0" || value == "10.1.0.255" || ipArray == null) 
    return false; 
else { 
    for (i = 0; i < 4; i++) { 
     thisSegment = ipArray[i]; 
     if (thisSegment > 254) { 
      return false;     
     } 
     if ((i == 0) && (thisSegment > 254)) { 
      return false; 
     } 
    } 
} 
return true; 


}, "Invalid IP Address"); 

$(document).ready(function() {  
    $("#agentsSearchForm").validate({ 
     rules: { 
      valueApiAddress: { 
       ipAddressFormat: true    
      }, 
      valueExternalApiAddress: { 
       ipAddressFormat: true     
      }, 
      valuePort: { 
       range: [1, 65535]     
      } 
     }, 
     messages: {  
      valuePort: { 
       range: "Valid port range is 1 to 65535." 
      } 
     } 
    }); 
}); 

感謝

回答

0

您可以進行驗證下面的代碼需要引用。您不需要添加其他驗證方法,它已經可用。

$("#saveForm").validate({ 
    rules: { 
     username: { 
      required: function (element) { 
       if ($('#ban_type option:selected').val() == 1) { 
        return true; 
       } else { 
        return false; 
       } 
      } 
     }, 
     from_ip: { 
      required: function (element) { 
       if ($('#ban_type option:selected').val() > 1) { 
        return true; 
       } else { 
        return false; 
       } 
      }, 
      ipv4: true 
     }, 
     to_ip: { 
      required: function (element) { 
       if ($('#ban_type option:selected').val() == 3) { 
        return true; 
       } else { 
        return false; 
       } 
      }, 
      ipv4: true 
     }, 
    }, 
    submitHandler: function() { 

    } 
}); 
相關問題