views:

112

answers:

2

I have the following code that makes sure inputs on forms aren't blank, however I have 2 forms on a page and only want this to check the inputs on the form called <form id="my_form"...

var valid_form = true;
$$('input').each(function(item){
    if( item.value == '' ) valid_form = false;
});

Please can somebody tell me how to do this?

+1  A: 

You can give a different ID for each of you forms like frmLogin and frmSearch and then when one of your forms is submitted you check only the inputs in it.

var valid_form = true;
$('form').submit(function () { 
 $(this).find('input').each(function(item){ 
   if( item.value == '' ) valid_form = false;  
 }) 
});

Or if you check your forms after a button has been clicked you can do it like this.

$('#yourButton').click(function () {
 $('#my_form input').each(function(item){ 
   if( item.value == '' ) valid_form = false;  
 }) 
});
Christian Toma
+2  A: 
var valid_form = true;
$$('#my_form input').each(function(item){
    if( item.value == '' ) valid_form = false;
});
chaos
work perfectly with $$ (not a typo!)