Page 1 of 1

Dynamic form validation

Posted: Tue Nov 17, 2009 11:12 pm
by pcoder
Hi All,
I tried a lots of way to validate a dynamic form. Validation is always works with the element ID right?
Sometimes, it seems like the validation is working with the element NAME. I give you this example.

Code: Select all

 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Dynamic Form Validation</title>
<script type="text/javascript" src="../lib/jquery.js"></script>
<script type="text/javascript" src="../jquery.validate.js"></script>
<script type="text/javascript">
    jQuery().ready(function(){
        var jsonRules = {
            rules: {
                full_name: {
                    required: true
                }
            }
        };
        jQuery("#test_form").validate(eval(jsonRules));
        var template = jQuery.format(jQuery("#template").val());
        function addRow(){
            jQuery(template(i++)).appendTo("#divPrimary");
            var j = i-1;
            jQuery("#address_"+j).rules("add", "required");
        }
        var i = 1;
        addRow();
        jQuery("#add").click(addRow);   
    });
    
</script>
</head>
 
<body>
<textarea id="template" style="display:none">
    <div id="template_id_{0}">
    <p>Address: <input type="text" name="address" id="address_{0}" /></p>
    </div>
</textarea>
<form action="" name="test_form" id="test_form" method="post">
<p>Name: <input type="text" name="full_name" id="full_name" /></p>
<div id="divPrimary"></div>
<p><input type="submit" name="Go" value="Go" /></p>
</form>
<button id="add">Add More</button>
</body>
 
Here, if you notice, I have added the dynamic content inside the textarea. And the address has the fixed NAME(address). But the ID increments dynamically.
Now, this dynamic form works perfectly when I increment the NAME like ID. If the validation is happening with the ID, then why I am not getting the validation?
Hope to get your views.

Thanks

Re: Dynamic form validation

Posted: Sat Nov 28, 2009 11:59 am
by josh
I use jquery.validate. I prefer the meta-data plugin support, you basically embed json into the class tag of the element. Keeps things nice and close to where they are actually used so you're not scanning up to the top of the .html template to look for what validations are added.

Re: Dynamic form validation

Posted: Sat Nov 28, 2009 1:53 pm
by califdon
I've never seen a <div> (and a <p>) element inside a <textarea> element. Is that even valid??

Re: Dynamic form validation

Posted: Mon Nov 30, 2009 11:24 pm
by pcoder
Thanks Josh for the reply. I think, Still this is not the solution. Rules will be overwrite because of the same NAME. Validations is always works with the element NAME.
And in my case, NAME is always same.:-(
I've never seen a <div> (and a <p>) element inside a <textarea> element. Is that even valid??
Yes Califdon, this is not the better way to implement. It's a kind of hacked way. Actually, I got this stuff from the jQuery validation section.