Aside

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

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="text/javascript">
function validate_signup(form) {
    if ( !document.getElementById('confirmoptin').checked ) {
        alert("You must confirm that you want to receive email from us");
        return false;
    }
return true;
}
</script>

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:

<script>
<!--
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;
}
//-->
</script>
</script>

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.

Related posts:

  1. PicApps gives bloggers access to licensed photographs for free
Stuff I Love

This website has been brought to you by the letters A, B and these fine products. Clicking and buying the stuff below will earn me a commission, thanks very much!

No Need For WordPress Developers — Drag & Drop With Headway Digging into WordPress Hosted on Site5
5 Responses to Simple form validation with FBJS on Facebook Page Tabs
  1. polat alemdar
    September 27, 2010 | 11:18 pm

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

    Thanks

    Polat

  2. Breklin
    December 24, 2010 | 2:25 am

    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
      December 24, 2010 | 7:59 am

      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.

  3. Kenny SAlter
    February 14, 2011 | 2:33 am

    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
      February 14, 2011 | 7:47 am

      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

Notify me of followup comments via e-mail. You can also subscribe without commenting.

If you are posting source code, please wrap it with [­code][/code] tags. For longer snippets of code you should use Pastebin to share your code and paste the link here. Thanks!