Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
intrepidclass opened this issue Nov 17, 2010 · 1 comment
Open

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

intrepidclass opened this issue Nov 17, 2010 · 1 comment

Comments

@intrepidclass
Copy link

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

@applecat
Copy link

Great solution, thanx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants