English 中文(简体)
Form Validation with FBJS
原标题:

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..

最佳回答

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.

问题回答

I am using simple code too. I can t figure out why when I submit with an error, I get the dialogue and the form goes ahead and submits anyway.


//....
var txt = Enter Zipcode ;
function  setError(){
    var obj=document.getElementById( mapsearch );
        obj.setValue(txt);
        obj.setStyle( color ,  red ); 
}
function valform(){
    var obj=document.getElementById( mapsearch );
    var val = obj.getValue();
    if(val!=   &&  !isNaN(val) && val.length>2 ){ 
        return true;
    } else {
        setError();
        (new Dialog()).showMessage( Zip Required ,  Please enter your zip code. );
        return false;
    }
}



...

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.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签