Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript trigger on "KeyPress" and "Paste"

Tags:

javascript

I have a JavaScript that checks if a field equals to 15 characters, and if not the submit button is greyed out. This works perfetcly if i type in 15 characters manually, but not if i paste 15 characters. How can i make it check even when content is pasted in the field?

Can i make it check for characters periodicly (seconds) or is there a function for "check on paste"?

My 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>
like image 656
hrdy Avatar asked Mar 11 '13 07:03

hrdy


3 Answers

Change keypress to keyup and it should work.

like image 156
zemirco Avatar answered Oct 22 '22 23:10

zemirco


Try something like

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

Don't use keypress event since it won't be fired while pressing backspace in IE/Chrome

like image 37
Arun P Johny Avatar answered Oct 22 '22 23:10

Arun P Johny


Tested, working. 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>
like image 23
fnkr Avatar answered Oct 22 '22 23:10

fnkr