I am looking for an auto-submit function for my form which should submit it once user enters text of certain length (lets say 15 chars) into an input field.
Is there any function in jQuery that would constantly (in very short intervals) check the length of the text in the input field and run a function once the required number of chars is met? Any ideas?
EDIT:
I am looking for this functionality:
User enters the page with the form - autofocuses on the input field - user reads a barcode with a scanner - then the barcode number is pasted in the input field (not keyup, keydown event) and the form is autosubmitted.
SOLVED:
Used this function to catch changes in input field:
$(#inputField).bind('input propertychange', function(){
dostuff();
});
Instead of checking for length of text in input field at particular interval
, lets attach keyup
event handler to the text field. Every time the keyup
event is fired, we'll check if the length exceeds 15. If it does then we'll submit the form with .submit()
API.
$('yourTextBoxSelector').on("change paste keyup", function(){
if($(this).val().length >15){
$('yourFormSelector').submit()
}
});
UPDATE : jQuery has an event paste
. You can use it...
Here is the way I would approach this. This also takes into account the user copying and pasting into the input area.
Jsfiddle -> http://jsfiddle.net/PgzrU/4/
Code below:
var max = 15;
$('input').change(function(e)
{
if ($(this).val().length > max) { alert('form fired') }
}).keyup(function(e)
{
if ($(this).val().length > max) { alert('form fired') }
});