views:

36

answers:

4

Hi,

Inside a tab, I have a form that is dynamically loaded via ajax. Since the name of the field is dynamic too(e.g. <input name='title1' id='title1', class='tRequired'>), I write a custom validation method inside the "on complete" like this. However, the custom code does not get executed (the alert never pops up) no matter what i try.

$.ajax
                (   
                    {   
                        url: 'index.php?func=trainingmgr&aAction=displayAddForm', <br>
                        type: 'GET',<br>
                        dataType: 'html',<br>
                        complete: function(req, err) 
                            {   
                                //Append response to the tab's body <br>
                                $(href, '#trainingTabs').append(req.responseText);

                                $.validator.addMethod
                                (   
                                    'tRequired', 
                                    function(value, element) 
                                    {   
                                        if(value == '')
                                        { 
                                          alert('I am empty'); <====== Never pops up
                                          return true; 
                                        }
                                        else return false;
                                    },  
                                    '<br>Required field'
                                );  

                                $('#upload' + index).click
                                (   
                                    function() 
                                    { $('#addForm' + index).validate(); }
                                );  
                            }   
                    }   
                );  
+1  A: 

Try

value == null
JeremySpouken
A: 

Similar to Jeremy's answer,

Try

value === ''
Damovisa
A: 

Hi, Thanks for your response. I tried both, and the alert still does not want to pop up. It does not pop up even when I tried this:
$.validator.addMethod (
'tRequired',
function(value, element)
{
alert('I am here...');
return (value == NULL) ? true: false;
},
'Required field' );
It seems that the custom validator (tRequired) does not get registered. I must be missing something silly here. Your thoughts are greatly appreciated. Please let me know if you need the whole js file to help me. Thanks.

Viet Pham
A: 

What IS the value when you get it then?
Check what it really is when you expect it to be blank/null/falsy, and figure out why it isn't.

npup