Skip to content

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

@intrepidclass

Description

@intrepidclass

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions