0

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?

1
  • What does your JavaScript console tell you? Commented Sep 27, 2013 at 13:29

2 Answers 2

1

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/

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. i use $("#commento_form_["+index+"]_commento").addClass("alphanumeric"); in each
i want alphanumeric with some special character. Thanks again
0

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
            })
        });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.