2013-03-11 16 views
6

Mam JavaScript, który sprawdza, czy pole ma 15 znaków, a jeśli nie, przycisk "wyślij" jest wyszarzony. Działa to perfekcyjnie, jeśli ręcznie wpisuję 15 znaków, ale nie, jeśli wkleję 15 znaków. Jak mogę to sprawdzić, nawet jeśli treść jest wklejana w polu?Wyzwalacz JavaScript na "KeyPress" i "Wklej"

Czy mogę okresowo sprawdzać znaki (w sekundach) lub czy istnieje funkcja "sprawdź wklej"?

Mój skrypt:

<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

Więcej kodu pod numerem – fnkr

+0

Co masz na myśli? To cały kod. Dodam też mój HTML powyżej, jeśli to pomoże? – hrdy

+0

Spróbuj zdarzenie typu "onpaste"? ''. Daj mi znać, jeśli to pomogło. – fnkr

Odpowiedz

9

Zmień keypress do keyup i powinno działać.

+0

To działało jak czar. Teraz sprawdza poprawność danych wejściowych na wejściach ręcznych i wklejonych. Dziękuję Ci! Poszedłem na to rozwiązanie, ponieważ jest najłatwiejsze i działało zarówno w IE, jak i Chrome. – hrdy

0

Należy użyć propertychange (IE) i input wydarzenia. 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

spróbować czegoś jak

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

Nie używaj keypress zdarzenie, ponieważ nie będą zwalniani naciskając backspace w IE/Chrome

1

testowane działa. 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>