I have a form that detects if all the text-fields are valid on each keyup() and focus(); if they\'re all valid, it will enable the submit button for the user to press. Howev
Myself I used
$(selector).on("change keyup blur input", function() {});
which did the trick in Chrome. input
is what made it work for autocomplete.
this is the ultimate solution, guaranteed to work
$(document).bind('mouseover', function(){
liveValidate();
});
I wanted a very good user experience on a field where it would not be invalid (turn red in my case) as long as the user was reasonably active e.g. still filling out the field.
To do this for normal input, I was able to hook up to keyup
with a debounce
function, while blur
is connected for immediate validation. While it appears that keyup
is triggered by lastpass, since I have debounced it, there was a delay in validation. Thanks to @peter-ajtai I tried to add the change
event and it indeed catches last pass and leaves the other niceties alone.
Coffeescript example:
@fieldExp
.keyup($.debounce(@_onExpChange, 3000))
.blur(@_onExpChange)
.change(@_onExpChange)
This worked well and lastpass form fill triggers immediate validation.
The answer has been given in this question. It doesn't use jQuery, but it works for Autocomplete:
Use js onpropertychange
event.
You could use the jQuery .change() function.
After the page initially loads, you can validate the entire form, just to check that it is in fact not filled in. After that you can use .change() to check if things have changed on the form, and if anything has changed, validate the form again.
$(document).ready(function() {
// validate form once, just to be sure (if valid, activate submit button)
});
...
<form>
<input class="target" type="text" value="Field 1" />
<select class="target">
<option value="option1" selected="selected">Option 1</option>
<option value="option2">Option 2</option>
</select>
</form>
<script type="text/javascript">
$('.target').change(function() {
alert('Something changed');
// Try validating form again (if valid, activate submit button)
});
</script>
Plan B
Another option is to always have the submit button clickable, but use .submit() to bind it to the form validator. Then if the form IS valid, carry on. If the form IS NOT valid use .preventDefault() to stop the submission of the form..... and you'd display a warning message too, indicating the missing fields.
The jQuery change event will only fire on blur. The keyup event will fire as you type. Neither fire on clicking an auto-completion option. I am also searching for a way to detect this, but I'm currently going with
$(selector).bind("change keyup",function(){
//Do something, probably with $(this).val()
});
But it doesn't quite solve the problem...