2010-07-06 10 views
13

C'è un buon plug-in di maschera IP per JQuery? Ho provato Masked Input Plugin ma non gli indirizzi IP con meno di 12 cifre. Quindi ho provato meioMask e questo non funziona con meno di 12 cifre. Eventuali suggerimenti?Ho bisogno di un plug-in Maschera IP JQuery

+0

cosa intendi "non funziona con meno di 12 cifre" ?? potresti mostrarmi qualche esempio ??? – Kai

+0

10.10.10.10 <12 cifre – HyderA

+0

IPv6 è più suppongo sia questo problema quindi non funziona con IPv4? Inoltre, si prega di inviare alcuni esempi/codice qualsiasi cosa per aiutare con quello che stai facendo non è possibile ottenere una risposta ragionevole senza una domanda ragionevole. Quindi tutto quello che posso dire è 42. – Sphvn

risposta

10

È possibile trovare la risposta in questo post:

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

e una demo per provare

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

+0

Nessuna documentazione. È un dolore cercare di lavorare. Recupero di valori per esempio. – HyderA

+0

Morto collegamento ... no 404 ma pagina vuota. – user9645

+0

Prova questo nel frattempo: http://web.archive.org/web/20140219174935/http://mlntn.com/2009/12/30/jquery-ip-address-plugin/ – Philippe

1

Gli esempi di lavoro dal plugin di ingresso mascherato -
http://digitalbush.com/projects/masked-input-plugin/

sono meno di 12 caratteri:

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

Hanno di lavoro che eseguono alla perfezione?

Qual è esattamente il tuo problema e puoi pubblicare più informazioni approfondite?

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

Stai cercando di contrastare per 1-3 cifre in ogni campo?

ad es. Per essere in grado di.

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

Se essere più descrittivo è possibile ottenere aiuto ..

Se siete dopo che si può provare qualcosa di più semplice da watermarking casella di input, piuttosto che imporre una maschera, in modo da poter variare i numeri che può essere inserito See Jquery-Filigrana - http://code.google.com/p/jquery-watermark/

+1

Sì, ho bisogno di 1-3 cifre in ogni campo, nessun numero specifico di cifre. E non sono sicuro di come il plugin per filigrana possa essere d'aiuto. – HyderA

+0

Qualcuno, per favore, spieghi la ragione per averlo svalutato? – Sphvn

+0

Non ho effettuato il downvoting, ma ho bisogno di sapere come abilitare il mascheramento di 1-3 cifre. – HyderA

1

i fou ND questo contemporaneamente non hanno bisogno di installare plugin

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

Questo è un messaggio più vecchio ma per chi vuole un modo semplice per manipolare più ingressi, senza l'utilizzo di un plugin di massa, o doversi preoccupare di documentazione o metodi , ecco un semplice metodo di selezione della classe che fa tutto per te. Solo IPv4, ma sembra che le tue esigenze siano piuttosto semplici.

//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 
}); 

Per rompere questo giù per la comprensione, si associa la classe "ip" nel vostro ingresso, gestirà il resto automaticamente: D Questa versione supporta la notazione CIDR (ad es: 192.168.1.1/16) consente solo indirizzi validi per essere entrata, per rimuovere CIDR funzione è possibile utilizzare utilizzare il seguente frammento di codice (non testato)

//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 
}); 

sto fornendo il codice qui per due scopi 1) Questo è qualcosa in cui credo deve essere affrontato, 2) spero di contribuire al mondo

Il frammento non è de firmato per essere smontato, né supportare IPv6, se hai bisogno di supporto IPv6 vedi https://code.google.com/p/jquery-input-ip-address-control/ suggerito da anyulled.

Ma a parte la sintassi complessa, rompe gli ottetti a parte, e controlla solo l'ottetto "attivo", che supporta qualsiasi indirizzo valido (0.0.0.0, 0.0.0.0/0, ect) in modo da utilizzare con saggezza non è così fare qualche stravagante controllo se non impedire un input non valido. Se stai cercando un correttore, per favore vedi il post di Santiago Elvira Ramirez sul validatore degli indirizzi IP.