rickharrison/validate.js

Breaking loop bug

NullDivision opened this issue · 2 comments

So, using the rule set:

{
    name:    'email',
    display: 'Email',
    rules:   'trim|required|xss_clean|valid_email'
}

The page blocked. The issue seems to stem from the for loops in the field validation method:

FormValidator.prototype._validateField = function(field) {
    // ...declarations

    for (var i = 0, ruleLength = rules.length; i < ruleLength; i++) {
        // ...validation code

        if (failed) {
            //  ...message allocation

            for (var i = 0; i < this.errors.length; i += 1) {
                if (field.id === this.errors[i].id) {
                    existingError = this.errors[i];
                }
            }

            // ...error storage
        }
    }
};

You're using a loop with index i and inside it re-declaring i. Might I suggesting changing the inner loop index to j or even better, move the if (failed) block to its own function for clarity?

Would you mind submitting a PR with a fix?

I've done a minimal modification but no update to the minified version since I couldn't find a build script for it.