1

I am trying to add some validation rules on an input created from html literals.

This is my fiddle: http://jsfiddle.net/idoxobi/HW4xY/1/

Html:

<form id='form' method='post' >
    <div id='details'>
    </div>
    <a id='Add' href='javascript:void()'>Add</a>
    <br />
    <input type='submit' />
</form>

JavaScript:

$(document).ready(function() {
    $('#form').validate();
    var input = "<input type='text' class='item' name='item' />";
    $('#Add').click(function() {
        $('#details').append(input+'<br />');
        $('#details input:last').rules('add', {
            required: true
        });
    });
})

It seems to recognize the rule just in the first element, but the others pass without validation.

Is the first time I use this plug-in, but in the forums everyone says this should work just fine.

Any suggestions?

2 Answers 2

1

check this:

html:

<form id='form' method='post' >
    <div id='details'>
    </div>
    <a id='Add' href='#'>Add</a>
    <br />
    <input type='submit' />
</form>

js:

$(document).ready(function() {
    $('#form').validate();
    var i = 0;
    $('#Add').click(function() {

        var input = "<input type='text' class='item' name='item"+ i++ +"' />";
        $('#details').append(input+'<br />');
        $('#details input:last').rules('add', {
            required: true
        });

        return false;
    });
});

on jsfiddle

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

Comments

0

Its because input field name is same for each, and it has been fixed by @YD1m, my method is as below:

$(document).ready(function() {
    $('#form').validate();
    var counter = 0;
    $('#Add').click(function() {
        counter++;
        $('#details').append("<input type='text' class='item required' name='item_" + counter + "' />" + '<br />');
    });
});

1 Comment

Thanks, the examples I saw before were just chunks of code with the "add rule" section and I didn't realized that also was necessary maintain an unique name in the inputs.

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.