2013-03-11 18 views
6

Possiedo un JavaScript che controlla se un campo è uguale a 15 caratteri e, in caso contrario, il pulsante di invio è disattivato. Funziona perfettamente se digito manualmente 15 caratteri, ma non se inserisco 15 caratteri. Come posso farlo controllare anche quando il contenuto viene incollato sul campo?Attivazione Javascript su "KeyPress" e "Paste"

Posso verificare periodicamente i caratteri (secondi) o esiste una funzione per "verifica su incolla"?

Il mio script:

<script type="text/javascript"> 
jQuery(document).ready(function($){ 
$('input[type=submit]').attr("disabled","disabled"); 
$('input[name="item_meta[1234]"]').keypress(function() { //change # to the ID of your field 
     if (document.getElementById("field_ygtw9u").value.length < 14) { //disable submit if "no" is selected 
       $('input[type=submit]').attr("disabled","disabled"); 
     } else { //enable the submit button 
       $('input[type=submit]').removeAttr('disabled'); 
     } 
    }); 
}); 
</script> 

<form> 
<div id="frm_field_1234_container" class="frm_form_field form-field frm_required_field frm_top_container"> 
<label class="frm_primary_label">Minimun 15 char: 
    <span class="frm_required"></span> 
</label> 
<input type="text" id="field_ygtw9u" name="item_meta[1234]" value="" maxlength="15" class="required"/> 

<p class="submit"> 
<input type="submit" value="Submit" /> 
</p> 
</form> 
+0

Codice a favore, – fnkr

+0

Cosa u significa? Questo è tutto il codice che c'è. Aggiungerò il mio HTML sopra anche se ciò aiuta? – hrdy

+0

Hai provato l'evento onpaste? ''. Fammi sapere se ha aiutato. – fnkr

risposta

9

Change keypress-keyup e dovrebbe funzionare.

+0

Questo ha funzionato come un fascino. Ora convalida l'input su input sia manuali che incollati. Grazie! È andato per questa soluzione poiché è la più semplice e ha funzionato sia in IE che in Chrome. – hrdy

0

si dovrebbe usare propertychange (IE) e input eventi. Live Demo

$('input[name="item_meta[1234]"]').bind("input", function() { //change # to the ID of your field 
    if (document.getElementById("field_ygtw9u").value.length < 14) { //disable submit if "no" is selected 
      $('input[type=submit]').attr("disabled","disabled"); 
    } else { //enable the submit button 
      $('input[type=submit]').removeAttr('disabled'); 
    } 
}); 

$('input[name="item_meta[1234]"]').bind("propertychange", function() { //change # to the ID of your field 
    if (document.getElementById("field_ygtw9u").value.length < 14) { //disable submit if "no" is selected 
      $('input[type=submit]').attr("disabled","disabled"); 
    } else { //enable the submit button 
      $('input[type=submit]').removeAttr('disabled'); 
    } 
}); 
1

provare qualcosa di simile

var $submit = $('input[type=submit]').attr("disabled","disabled"); 
$('input[name="item_meta[1234]"]').keyup(function() { //change # to the ID of your field 
    var $this = $(this); 
    if ($this.val().length < 14) { //disable submit if "no" is selected 
     $submit.attr("disabled","disabled"); 
    } else { //enable the submit button 
     $submit.removeAttr('disabled'); 
    } 
    }); 

$('#field_ygtw9u').on('paste', function(e){ 
    var $this = $(this); 
    setTimeout(function() { 
     if ($this.val().length < 14) { //disable submit if "no" is selected 
      $submit.attr("disabled","disabled"); 
     } else { //enable the submit button 
      $submit.removeAttr('disabled'); 
     } 
    }, 0); 
}); 

Demo: Fiddle

Non utilizzare keypress evento dal momento che non sarà licenziato premendo backspace in IE/Chrome

1

Provato, lavorando. Fiddle: http://jsfiddle.net/fnkr/MXnuk/

<script type="text/javascript"> 
    function checkInput() { 
     if (document.getElementById("field_ygtw9u").value.length < 14) { //disable submit if "no" is selected 
       $('input[type=submit]').attr("disabled", "disabled"); 
      } else { //enable the submit button 
       $('input[type=submit]').removeAttr('disabled'); 
      } 
    } 

    jQuery(document).ready(function ($) { 
     $('input[type=submit]').attr("disabled", "disabled"); 
     $('input[name="item_meta[1234]"]').bind("input", function() { //change # to the ID of your field 
      checkInput();  
     }); 

     $('input[name="item_meta[1234]"]').bind("propertychange", function() { //change # to the ID of your field 
      checkInput(); 
     }); 
    }); 
</script> 

<form> 
    <div id="frm_field_1234_container" class="frm_form_field form-field frm_required_field frm_top_container"> 
     <label class="frm_primary_label">Minimun 15 char: <span class="frm_required"></span> 

     </label> 
     <input type="text" id="field_ygtw9u" name="item_meta[1234]" value="" maxlength="15" 
     class="required" /> 
     <p class="submit"> 
      <input type="submit" value="Submit" /> 
     </p> 
</form>