Search code examples
facebookxfbml

how to avoid validations for optional fields


I created a XFBML form with validations. There are couple of fields which are optional for end user. But as soon as the validations are enabled the form expect all the fields to be filled. So, how to skip required validation for optional fields.

The code currently looks like:

<fb:registration redirect-uri="http://www.sakshum.org/FbBloodDonorRegister" fields='[{"name":"name"},{"name":"first_name"},{"name":"last_name"}, {"name":"gender"}, {"name":"birthday"},{"name":"email"}, {"name":"cellPhone", "description":"Cell Number", "type":"text"}, {"name":"homePhone", "description":"Home Number", "type":"text"}, {"name":"officePhone", "description":"Office Number", "type":"text"}, {"name":"primaryAddress", "description":"Primary Address", "type":"text"}, {"name":"area", "description":"Locality/Village/Area", "type":"text"},{"name":"location"}]'  onvalidate="validated" width="530">
</fb:registration>
<script> 
function validated(form) {
    errors = {};    
    if(form.cellPhone.trim().length != 10){
        errors.cellPhone = "Cell number is required and should be 10 of digits";
    }

    if(form.homePhone.trim().length > 0){
        if(form.homePhone.trim().length != 10)
            errors.homePhone = "Home number should be 10 of digits";
    }   

    if(form.officePhone.trim().length > 0){
        if(form.officePhone.trim().length != 10)
            errors.officePhone = "Office number should be 10 of digits";
    }   

    if(form.homePhone.trim().length > 0 || form.officePhone.trim().length > 0){
        if(form.homePhone.trim() == form.officePhone.trim() || form.homePhone.trim() == form.cellPhone.trim() || form.officePhone.trim() == form.cellPhone.trim()){
            errors.homePhone = "Cell number, office number and home number cannot be same";
            errors.officePhone = "Cell number, office number and home number cannot be same";
        }
    }

  return errors;
}
</script> 

Solution

  • Looks like a known bug to facebook https://developers.facebook.com/bugs/139030226234017?browse=search_50551341bcf073b68986277