Barno
Barno

Reputation: 3331

jQuery Validate within jQuery .each()

I have many form create in for

I have this HTML

<form method="post"  id="form_commento-[i]" class="form_fancybox_commenti">
    <div class="form-section">
        <span style="position: relative">
            <input type="text" id="commento_form_[i]_commento" name="commento_form_[i][commento]" required="required"/>
            <button type="submit" class="submit-button" id="submit_form_commenti">Commenta</button>
        </span>
    </div>
</form>

Where [i] is index

in my document ready I have

$(document).ready(function() {

    jQuery.validator.addMethod("alphanumeric", function (value, element) {
        return this.optional(element) || /^[a-zA-Z0-9\n\-'àèìòù: <_,. !?()]*$/.test(value);
    },   "error");

    $('.form_fancybox_commenti').each(function(index, numero_form) {

    var theRules = {};

    theRules[
        'commento_form_['+index+'][commento]'] = {alphanumeric: true};


    $(this).validate({
        rules: theRules,
        submitHandler: function(form) {
            save(form);
        }
    });
});

but my custom rules doesn't works.

Anyway to resolve this problem?

Upvotes: 0

Views: 3342

Answers (2)

Chris Sim
Chris Sim

Reputation: 4132

This is the Best solution I found and I'am currently using in my project:

        // Adding rule to each item in commento_form_i list 
        jQuery('[id^=commento_form_]').each(function(e) {
        jQuery(this).rules('add', {
            minlength: 2,
            required: true
            })
        });

Upvotes: 0

Sparky
Sparky

Reputation: 98738

If the name is commento_form_[i][commento], then you're missing a set of brackets here...

'commento_form_'+index+'[commento]'

=>

'commento_form_['+index+'][commento]'

However, at this point, you have not yet defined index so this method fails with a JavaScript console error.


There is a very simple alternative to that chunk of JavaScript. Add class="alphanumeric" to your <input> elements, and your code is reduced simply to this:

$(document).ready(function () {

    jQuery.validator.addMethod("alphanumeric", function (value, element) {
        return this.optional(element) || /^[a-zA-Z0-9\n\-'àèìòù: <_,. !?()]*$/.test(value);
    }, "error");

    $('.form_fancybox_commenti').each(function (index, numero_form) {
        $(this).validate({
            submitHandler: function (form) {
                save(form);
                // alert('save form: ' + index); // for demo
                return false; // blocks default form action
            }
        });
    });

});

DEMO: http://jsfiddle.net/XTtTP/


If you'd rather use JavaScript for assigning your rules, you can also use the .rules('add') method within a jQuery .each(), as follows, and no indexing is required:

$('input[name^="commento_form_"]').each(function () {
    $(this).rules('add', {
        alphanumeric: true
    });
});

DEMO: http://jsfiddle.net/T776Z/


BTW, there is already a method called alphanumeric in the additional-methods.js file. See: http://jsfiddle.net/h45Da/

Upvotes: 1

Related Questions