views:

2444

answers:

4

Hello,

I've a form validation problem with FBJS to use on Facebook application. I checked out validation examples on documentation and I can check textbox values with form.serialize(); but unfortunetely I couldn't figure out how to check dropdown and checkbox values.

Thanks in advance..

+1  A: 

For check boxes and radio buttons use code like this:

if (document.getElementById("checkbox_or_radio_button_id_here").getChecked() == true)
{
 // yes it was checked
}

For dropdown:

if (document.getElementById("dropdown_id_here").getValue() != '')
{
 // yes dropdown was not empty
}

I personally don't use serialize in facebook validation, i just use simple code as above.

Thanks, hope that helps.

Sarfraz
yeah! this works far more better...
Harish Kurup
A: 

I found a method that while not very elegant it is simple and works pretty well using serialize().

my form code snippet:

<select name="state" id="states" class="required">

<option value=""></option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>

</select>

<input value="radio1" type="radio" name="radioPicks" class="radio"/>
<input value="radio2" type="radio" name="radioPicks" class="radio"/>
<input value="radio3" type="radio" name="radioPicks" class="radio"/>
<input type="hidden" name="radioPicks" value="">

The FBJS needed to get this to work:

<script type="text/javascript">
<!--

 

function checkForm(form) {

var params=form.serialize();

if (params.state.length>0 && params.radioPicks!="")

return true;

else

var myDialog = new Dialog(Dialog.DIALOG_POP);
myDialog.showMessage('Almost Done!', 'Please complete all fields', button_confirm='Close');

return false;

}
-->
</script>

The key to this validation method relies on a empty option tag to return "" for the selected tag value and a "" for the radio buttons. I added a hidden input and named it the same name as the radio buttons to add its data to the array returned. the "" value is used in the conditional to check and say if these fields are not empty give the form the ok to send the info to the server. I hope this helps.

The Orange Mantis
A: 
sameer
A: 
GoodNews