2010-07-06 95 views
13

JQuery有一個好的IP Mask插件嗎?我試過Masked Input Plugin,但它沒有少於12位的IP地址。然後我嘗試了meioMask,這不能用少於12位的數字。有什麼建議麼?我需要一個JQuery IP Mask插件

+0

你是什麼意思「不少於12位數字」?你能告訴我一些例子嗎? – Kai 2010-07-06 07:41:59

+0

10.10.10.10 <12位數字 – HyderA 2010-07-19 05:48:32

+0

IPv6更多我猜是這個問題,所以它不適用於IPv4?另外請張貼一些例子/代碼來幫助你做什麼,你不能得到一個合理的答案與一個合理的問題。所以我只能說42. – Sphvn 2010-07-19 05:53:08

回答

10

你可以找到在這個崗位你的答案:

http://mlntn.com/2009/12/30/jquery-ip-address-plugin/

,併爲你演示嘗試

http://mlntn.com/demos/jquery-ipaddress/

+0

沒有文件。嘗試和工作是一種痛苦。例如檢索值。 – HyderA 2010-07-19 07:39:31

+0

死鏈接...沒有404,但空白頁。 – user9645 2014-09-03 16:53:53

+0

在此期間嘗試此操作:http://web.archive.org/web/20140219174935/http://mlntn.com/2009/12/30/jquery-ip-address-plugin/ – Philippe 2014-09-25 19:49:58

1

從蒙面輸入插件的工作實例 -
http://digitalbush.com/projects/masked-input-plugin/

少於12個字符:

jQuery(function($){ 
    $("#date").mask("99/99/9999"); 
    $("#phone").mask("(999) 999-9999"); 
    $("#tin").mask("99-9999999"); 
    $("#ssn").mask("999-99-9999"); 
}); 

他們的工作實例完美運行?

你的問題是什麼,你可以發佈更深入的信息?

jQuery(function($){ 
    $("#MyElementID").mask("10.0.0.0"); //Does this not work? 
}); 

你想在每個字段計數1-3位數嗎?

例如能夠。

$("#MyElementID").mask("1.0.0.0"); //this 
$("#MyElementID").mask("10.10.10.10"); //or this 
$("#MyElementID").mask("100.100.100.100"); //or this 

如果你是更具描述性的,你可以得到幫助..

如果你以後,你可以嘗試一些簡單的水印輸入框,而不是強制執行面膜,讓你可以改變的數字,可以輸入。 見jQuery的水印 - http://code.google.com/p/jquery-watermark/

+1

是的,我需要1-3個數字在每個領域,沒有具體的數字位數。而且我不確定水印插件如何提供幫助。 – HyderA 2010-07-19 06:06:31

+0

有人請解釋說明這個的原因嗎? – Sphvn 2010-07-19 07:23:32

+0

我沒有downvote,但我需要知道如何允許屏蔽1-3位數字。 – HyderA 2010-07-19 07:37:09

1

i fou第二這一點,你鴕鳥政策需要安裝插件

function fnValidateIPAddress(ipaddr) { 
    //Remember, this function will validate only Class C IP. 
    //change to other IP Classes as you need 
    ipaddr = ipaddr.replace(/\s/g, "") //remove spaces for checking 
    var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/; //regex. check for digits and in 
              //all 4 quadrants of the IP 
    if (re.test(ipaddr)) { 
     //split into units with dots "." 
     var parts = ipaddr.split("."); 
     //if the first unit/quadrant of the IP is zero 
     if (parseInt(parseFloat(parts[0])) == 0) { 
      return false; 
     } 
     //if the fourth unit/quadrant of the IP is zero 
     if (parseInt(parseFloat(parts[3])) == 0) { 
      return false; 
     } 
     //if any part is greater than 255 
     for (var i=0; i<parts.length; i++) { 
      if (parseInt(parseFloat(parts[i])) > 255){ 
       return false; 
      } 
     } 
     return true; 
    } else { 
     return false; 
    } 
} 
3

但是,這是一個較舊的職位的人誰希望有一個簡單的方法來處理多個輸入,而不使用膨脹插件,或者不必擔心文檔或方法,這裏有一個簡單的類選擇器方法,它可以完成所有這些。它只有IPv4,但它聽起來像你的需求非常簡單。

//jQuery 1.9+ selector pattern, 
//To get working with an older version 
//Swap first line to $(".ip").bind('keydown',function(e){ 
//To get working with jQuery versions support .live 
//$(".ip").live('keydown',function(e){ 
$(document).on('keydown',".ip",function(e){ 
    var code = e.keyCode || e.which; 
    var sections = $(this).val().split('.'); 
    //Only check last section! 
    var isInt  = ((code >= 48 && code <= 57) || (code >= 96 && code <= 105)); 
    var hasSlash = $(this).val().indexOf("/") == -1; 
    if(isInt){ 
     if(hasSlash){ 
      if(sections.length < 4){ 
       //We can add another octet 
       var val = parseInt(sections[sections.length-1]+String.fromCharCode(code)); 
       if(val > 255 || parseInt(sections[sections.length-1]) == 0){ 
        $(this).val($(this).val()+"."+String.fromCharCode(code)); 
        return false; 
       } 
       return true; 
      } else { 
       //Lets prevent string manipulations, our string is long enough 
       var val = parseInt(sections[sections.length-1]+String.fromCharCode(code)); 
       if(val > 255 || parseInt(sections[sections.length-1]) == 0){ 
        return false; 
       } 
       return true; 
      } 
     } else { 
      var cidr_split = $(this).val().split('/'); 
      var target_val = parseInt(cidr_split[1]+String.fromCharCode(code)); 
      return (target_val < 33 && target_val.toString().length < 3 && parseInt(cidr_split[1]) != 0); 
     } 
    } else if(code == 191){ 
     //CIDR Slash 
     return ($(this).val().indexOf("/") == -1); 
    } else if(code == 8 || code == 46 || code == 9 || code == 13){ 
     return true; 
    } 
    return false 
}); 

爲了打破這種下來理解,您綁定在你的輸入類「IP」,它會自動完成剩下的事情:d該版本支持CIDR標記(如:192.168.1.1/16)只允許有效的地址輸入,刪除CIDR功能,您可以使用使用下面的代碼片段(未測試)

//jQuery 1.9+ selector pattern, 
//To get working with an older version 
//Swap first line to $(".ip").bind('keydown',function(e){ 
//To get working with jQuery versions support .live 
//$(".ip").live('keydown',function(e){ 
$(document).on('keydown',".ip",function(e){ 
    var code = e.keyCode || e.which; 
    var sections = $(this).val().split('.'); 
    //Only check last section! 
    var isInt  = ((code >= 48 && code <= 57) || (code >= 96 && code <= 105)); 
    if(isInt){ 
     if(sections.length < 4){ 
      //We can add another octet 
      var val = parseInt(sections[sections.length-1]+String.fromCharCode(code)); 
      if(val > 255 || parseInt(sections[sections.length-1]) == 0){ 
       $(this).val($(this).val()+"."+String.fromCharCode(code)); 
       return false; 
      } 
      return true; 
     } else { 
      //Lets prevent string manipulations, our string is long enough 
      var val = parseInt(sections[sections.length-1]+String.fromCharCode(code)); 
      if(val > 255 || parseInt(sections[sections.length-1]) == 0){ 
       return false; 
      } 
      return true; 
     } 
    } else if(code == 8 || code == 46 || code == 9 || code == 13){ 
     return true; 
    } 
    return false 
}); 

我在這裏提供的代碼有兩個目的1)這是我認爲需要解決的問題,2 )我希望對世界做出貢獻

這段代碼不是de簽署拆分,也不支持IPv6,如果您需要IPv6支持,請參閱已提示的https://code.google.com/p/jquery-input-ip-address-control/

但是除了複雜的語法之外,它將八位字節分開,只檢查「活動」字節,它支持任何VALID地址(0.0.0.0,0.0.0.0/0等),所以明智地使用它不會除防止無效輸入之外,不要做任何奇怪的檢查。如果您正在尋找一個檢查器,請參閱Santiago Elvira Ramirez關於IP地址驗證器的文章。