django-ajax-validation icon indicating copy to clipboard operation
django-ajax-validation copied to clipboard

Delayed validation would be nice, here's how I do it now

Open intrepidclass opened this issue 14 years ago • 1 comments

It would be nice if you could tell the validation function that it should wait 500 ms after the last onkey event before it starts validating. That way you have almost instant feedback on what you're typing, but you're not sending a request to the server for every character.

Here's how I'm doing it now:


<script type="text/javascript">
    $(document).ready(function()    {

        var timers = [];
        var selectorAndFields = [['#id_username',['username']], 
                                ['#id_email',['username','email']], 
                                ['#id_password1',['username','email','password1']], 
                                ['#id_password2',['username','email','password2', 'password1']]];

        for (i = 0; i < selectorAndFields.length; i++) {
            // bind the validation function to the change event for the input field.
            $('#signUpForm').validate('{% url registration_form_validation %}', {type: 'table', fields: selectorAndFields[i][1], dom: $(selectorAndFields[i][0]), event: 'change'});

            // trigger a change event 500 msecs after the last keyup-event for the same input field.
            $(selectorAndFields[i][0]).keyup(function() {
                var field = $(this);
                if (timers[i]) { clearTimeout(timers[i]); }
                timers[i] = setTimeout(function(){field.trigger('change')},500);
            });
        }
        
    });
</script>

I've set it up so that it validates the field in which the user is typing but also all the fields above. (I thought it would be silly to get feedback on fields you haven't gotten to typing into yet).

intrepidclass avatar Nov 17 '10 17:11 intrepidclass

Great solution, thanx!

applecat avatar Jun 21 '11 07:06 applecat