如何避免可选字段的验证

时间:2012-09-14 22:29:43

标签: facebook xfbml

我创建了一个带有验证的XFBML表单。最终用户可以选择几个字段。但是一旦启用了验证,表单就会期望填写所有字段。那么,如何跳过对可选字段的必要验证。

目前的代码如下:

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

1 个答案:

答案 0 :(得分:0)