Simple form validation with FBJS on Facebook Page Tabs

Today I was working on a custom Facebook Page which included a sign up form. On the sign up form was a checkbox with an id of confirmoptin that needed to be checked before the form could be submitted:

<form action=&quot;http://..&quot; method=&quot;post&quot; id=&quot;subForm&quot; onsubmit=&quot;return validate_signup(this)&quot;>
<!-- email address -->
<input class=&quot;text&quot; type=&quot;text&quot; name=&quot;email&quot; id=&quot;email&quot; /><br/>
<!-- checkbox to confirm opt-in -->
<input class=&quot;checkbox&quot; type=&quot;checkbox&quot; id=&quot;confirmoptin&quot; name=&quot;confirmoptin&quot; />
<label for=&quot;confirmoptin&quot;>Yes, I want to receive email!</label><br/>
<input type=&quot;submit&quot; value=&quot;Subscribe&quot; />

With plain ol’ JavaScript, you can easily do a simple validation. This code will show a JavaScript alert window if the user didn’t tick the checkbox:

<script type=&quot;text/javascript&quot;>
function validate_signup(form) {
    if ( !document.getElementById('confirmoptin').checked ) {
        alert(&quot;You must confirm that you want to receive email from us&quot;);
        return false;
return true;

On Facebook Pages, plain ol’ JavaScript is not allowed. Instead you have to use FBJS, which is very similar, but has got some modifications so it works with FBML and prevents dangerous hacks.

In the example above, the alert() function and checked object is not supported in FBJS. The code below translates these two unsupported functions into Dialog and getChecked respectively:

function validate_signup(form) {
	if ( document.getElementById('confirmoptin').getChecked() == false ) {
		(new Dialog()).showMessage('Confirmation Required', 'You must confirm you want to receive email from Rotiboy.');
		return false;
return true;

In plain English, the code above is triggered when a user tries to submit the form. The code looks for the element with the id of confirmoptin and checks if it is checked (selected). If it is not selected (false), display the Confirmation Required message in a Facebook-style dialog box (pictured at the top of this article) and prevent the form submission. If it is checked, just return true and submit the form.

Hopefully the code above will help a few people who are scratching their head about the same thing. See the form in action on the Rotiboy Malaysia Welcome Tab.

5 Comments on "Simple form validation with FBJS on Facebook Page Tabs"

  • polat alemdar says

    Thank you very much. I wish you become successfull in your career.



  • Breklin says

    Are you using this form with AJAX? I have a similar set up that circumvents the access permissions request and submits the data to the FBJS Ajax call. However, when I try to implement a wrapper that validates the form data then, if there are no errors, it submits the form via FB Ajax to a PHP file on our server; it simply fails to submit or sends the user to the page directly – no Ajax submission takes place. Can you assist me on this issue?

    • blogjunkie says

      Hi Breklin, no I’m not submitting the form via AJAX. This tutorial only talks about validating the form with FBJS.

      Actually I’m not sure you can submit AJAX calls from Facebook pages – you should double check that.

  • My site is built with headway and I would like the form to use Ajax so the subscriber can subscribe without leaving the current page.

    I can add html and php using a leaf but not sure how to employ javascript with headway. Most of the solutions I’ve seen use Javascript. Any ideas how to make this work?

    • blogjunkie says

      Hi Kenny, the J in AJAX stands for JavaScript so you definitely need it. If your JS is enclosed in <script type="text/javascript">...</script> you can paste it into the HTML/PHP leaf. Ask the friendly people in the support forum for more details. Good luck!

Leave a Reply

%d bloggers like this: