You will recieve your password to this address. Address is not made public.

Your preferred username that is used when logging in.

How to validate dynamically added form elements? Created May 13, 2010

This thread is solved

Views: 6140     Replies: 17     Last reply Dec 7, 2011  
You must login first before you can use this feature

jaxhoo

Posts: 3

Registered:
May 13, 2010

How to validate dynamically added form elements?

Posted: May 13, 2010

I want to validate form elements that are being dynamically added via javascript. I am having trouble getting Validator to recognize the custom inputs. Is there something I am missing?

Tero
Author of jQuery Tools and this website + JavaScript developer of Flowplayer.

Posts: 1867

Registered:
Nov 16, 2007

» How to validate dynamically added form elements?

Posted: May 15, 2010

Reply to: How to validate dynamically added form elements?, from jaxhoo
should not be a problem. you sould first add the inputs and then initialize the validator.

jaxhoo

Posts: 3

Registered:
May 13, 2010

» » How to validate dynamically added form elements?

Posted: May 17, 2010

Reply to: » How to validate dynamically added form elements?, from tipiirai
I have already tried this and it did not work.

The page has a single set of elements built in to the initially rendered html. I initialize the validator at this point and it works on that first row. At the user's request I create additional sets of form elements, and re-initialize the validator, but the additional form elements are not being validated.

Tero
Author of jQuery Tools and this website + JavaScript developer of Flowplayer.

Posts: 1867

Registered:
Nov 16, 2007

» » » How to validate dynamically added form elements?

Posted: May 19, 2010

Reply to: » » How to validate dynamically added form elements?, from jaxhoo
try to re-initialize like this


$("selector-to-my-form").data("validator", null).validator({ 
  // config
});

jaxhoo

Posts: 3

Registered:
May 13, 2010

» » » » How to validate dynamically added form elements?

Posted: May 21, 2010

Reply to: » » » How to validate dynamically added form elements?, from tipiirai
That does not work either. I've only tried it with 1.2.1 though since the 1.2.2 download doesn't work.

Update: I have tested this with 1.2.2 as well. It still does not work on the dynamically created form elements.

Dcodex

Posts: 5

Registered:
May 26, 2010

» How to validate dynamically added form elements?

Posted: May 26, 2010

Reply to: How to validate dynamically added form elements?, from jaxhoo

function checkForm(id)
{
    var inputs = typeof id !== undefined ? $('#'+id+' input[required]:not(:hidden)') : $('input[required]:not(:hidden)');

    if (inputs) {
        if (typeof inputs.data == 'function' && typeof inputs.data('validator') == 'object') {
            inputs.data('validator').reset(inputs);
            inputs.data('validator', null);
        }
        inputs.validator(
            //config
        );

        return inputs.data("validator").checkValidity(); 
    }
}

Federico

Posts: 1

Registered:
May 27, 2010

fails when remove elements

Posted: May 27, 2010

Reply to: » How to validate dynamically added form elements?, from Dcodex
Hi Dcodex, your script run ok.. but fail when I remove a dynamic added element. I'm tried to resolve but I cant't did it :(

Thanks, Federico

Dcodex

Posts: 5

Registered:
May 26, 2010

» fails when remove elements

Posted: May 27, 2010

Reply to: fails when remove elements, from fedegonzal
found bug with typeof inputs.data('validator') == 'object' i forgot null is an object too.
add:

if (typeof inputs.data == 'function' && typeof inputs.data('validator') == 'object' && inputs.data('validator') !== null) {
    //...
}
i tryt this:

<form id="myForm">
    <input name="input[1]" type="text" required="required" />
    <input name="input[2]" type="text" required="required" />
    <input name="input[3]" type="text" required="required" />
</form>
<script type="text/javascript">
    console.log(checkForm('myForm'));
    $('#myForm').append('<input required="required">');
    console.log(checkForm('myForm'));
    $('#myForm input').val('dummy');
    console.log(checkForm('myForm'));
    $('#myForm input:last').remove();
    console.log(checkForm('myForm'));
    $('#myForm input').val('');
    console.log(checkForm('myForm'));
</script>
result:

false
false
true
true
false

working ok for me ?

jeanj

Posts: 2

Registered:
Jun 9, 2010

» » fails when remove elements

Posted: Jun 9, 2010

Reply to: » fails when remove elements, from Dcodex
hi Dcodex,

thanks, the function is working for me (apart from the fact that input[required] of course will fail with select fields).

Nevertheless, I would hope that the functionality of working with dynamically added elements will be implemented into the core, so that no additional function call will be necessary.

thanks,

jean

Tero
Author of jQuery Tools and this website + JavaScript developer of Flowplayer.

Posts: 1867

Registered:
Nov 16, 2007

» How to validate dynamically added form elements?

Posted: Jun 11, 2010

Reply to: How to validate dynamically added form elements?, from jaxhoo
Hello,

Version 1.2.3 will have a fix for this situation. You can do following


// initialize validator
$("form").validator(); 

// add new fields
$("form").append('<input type="email" value="foo" />');

// re-initialize and the new fields are taken into account
$("form").validator(); 

Additionally a new destroy method is introduced that will completely remove the validator instance. The new version is already in Github and here is the commit for this change:

http://github.com/jquerytools/jquerytools/commit/f2ffafa9a34ea4ff3b5c80e51db22deb60fe9e8c

be introducing a new destroy() method

origamisan

Posts: 3

Registered:
Dec 7, 2010

» » How to validate dynamically added form elements?

Posted: Dec 7, 2010

Reply to: » How to validate dynamically added form elements?, from tipiirai
This seems to be broken in 1.2.5. Can anyone confirm this?

lichmax.com

Posts: 2

Registered:
Dec 7, 2010

» » » How to validate dynamically added form elements?

Posted: Dec 7, 2010

Reply to: » » How to validate dynamically added form elements?, from origamisan
It is broken for me too.

origamisan

Posts: 3

Registered:
Dec 7, 2010

» How to validate dynamically added form elements?

Posted: Dec 8, 2010

Reply to: How to validate dynamically added form elements?, from jaxhoo
Anyone know how to get this working in 1.2.5?

origamisan

Posts: 3

Registered:
Dec 7, 2010

» How to validate dynamically added form elements?

Posted: Dec 15, 2010

Reply to: How to validate dynamically added form elements?, from jaxhoo
Anyone have this working in 1.2.5?

AbraaoAlves

Posts: 1

Registered:
Dec 7, 2011

» How to validate dynamically added form elements?

Posted: Dec 7, 2011

Reply to: How to validate dynamically added form elements?, from jaxhoo
Anyone have this working in 1.2.6?

alibby

Posts: 1581

Registered:
Jun 2, 2010

» » How to validate dynamically added form elements?

Posted: Dec 7, 2011

Reply to: » How to validate dynamically added form elements?, from AbraaoAlves
Hi,

Do you have a standalone demo of code, where it shows the issue for you please? I would like to see if I can help get this working??