jQuery validation: required and minlength for dynamic fields

This is not a smart way though, but I tried to code for a bit complexed form.

Demo.

Code snippet:

$('form input[type=button]').click(function(){
    $('ul').show().append('<li><input class="hidden" type="checkbox" name="list[]" value="1" checked="checked" />'
        + $('select[name=item_name] option:selected').html() + '&nbsp;'
        + $('select[name=price] option:selected').html() + '&nbsp;<a>remove</a></li>');
    $('ul li a').click(function(){
        $(this).parent().fadeOut('slow', function(){
            $(this).remove();
        });
    });
});
var validator = $('form').validate({
    rules: {
        'list[]': { required: true, minlength: 2 }
    },
    submitHandler: function(){
        alert('submitted!');
        return false;
    }
});

Using jQuery 1.4.2 and validation plugin as well.

This entry was posted in Tweak and tagged , . Bookmark the permalink. Both comments and trackbacks are currently closed.