使用Facebook注册XFBML进行客户端验证

时间:2012-02-29 22:36:41

标签: facebook validation registration

我正在使用嵌入HTML5文档中的XFBML。我按照Facebook网站上“注册高级用途”文档中提供的说明设置了客户端验证,但它只显示模糊错误,并且在我点击“注册”按钮时不会验证(所以如果我填写错误的字段并在另一个元素上单击外部,它会显示错误,但是我可以单击“注册”按钮,它会注册我。

我在JS调试器中跟踪它,并通过添加警报/日志语句,我的validate_registration函数只被调用onblur,而且从未点击“注册”按钮。

有人看过这种行为,知道我在这里缺少什么吗? Facebook在“注册高级用途”中的实例非常有效,所以我知道我在某个地方犯了错误。

我的XFBML标签是这样的:

<div class="fb-registration"
    onvalidate="validate_registration"
    fields="[
      {'name': 'name'},
      {'name': 'first_name'},
      {'name': 'gender'},
      {'name': 'email'},
      {'name': 'user_type', 'description': 'Are you a?', 'type': 'select',
        'options': {
          'parent': 'Parent/Guardian looking for childcare',
          'sitter': 'Babysitter/Nanny looking for sitting jobs'
        }
      },
      {'name': 'zip_code', 'description': 'Zip code', 'type': 'text'},
      {'name': 'over_eighteen', 'description': 'I am at least 18 years old', 'type': 'checkbox' }
    ]"
    fb_only="true"
    redirect-uri="<%= parse_fb_connect_signed_request_users_url %>"
    width="530">
  </div>

我的验证功能是:

function validate_registration (custom_fields) {
    var errors = {},
        regexZipCode = /^\d\d\d\d\d$/;

    console.log("in validate_registration");

    if (custom_fields.user_type !== 'parent' && custom_fields.user_type !== 'sitter') {
        errors.user_type = "Select either Parent or Sitter";
    }

    if ( ! regexZipCode.test(custom_fields.zip_code) ) {
        errors.zip_code = 'Enter your 5-digit U.S. zip code';
    }

    if ( ! custom_fields.over_eighteen ) {
        errors.over_eighteen = 'You must be at least 18 years old to use this site!';
    }

    return errors;
}

1 个答案:

答案 0 :(得分:3)

所以,别介意。

我收到的消息是“您刚刚使用Facebook帐户注册了X. 如果您不打算这样做,可以在下面撤消。“并且认为这意味着我已经注册了。但是如果我点击”继续“,它会显示所有验证错误,因此它可以正常工作 - 它只是在最后一步之后才会生效。

相关问题