CraftyClicks.co.uk

JavaScript postcode/address finder class - user guide

This is a generic user guide to using our JavaScript. As standard the JavaScript class allows easy integration of RapidAddress and FlexiAddress into any HTML page. Feel free to contact us should you need any help with integration.

The class

Code for the class can be downloaded from the downloads section.

If using the compressed version of the class, charset needs to be set to "ISO-8859-1", eg:

<script type="text/javascript" charset="ISO-8859-1" src="crafty_postcode.class.js">
</script>

Configuration

To create and configure a JavaScript object:

<script>
  var cp_obj = CraftyPostcodeCreate();
  cp_obj.set("access_token", "xxxxx-xxxxx-xxxxx-xxxxx"); // your token here
  
  cp_obj.set("option_1", "value_1");
	...
  cp_obj.set("option_n", "value_n");
</script>

If you want to use address lookup more than once on a page, you can create multiple objects. Each object can be used independently of the others :

  var cp_obj_1 = CraftyPostcodeCreate();
	....
  var cp_obj_n = CraftyPostcodeCreate();

Available options:

Option NameDescription
access_tokenyour web service access token
result_elem_idid of the element where the result drop down box, or notice/error messages will be placed
formname of the form containing address fields, if set elem_* options are set to names of the form fields; if left blank elem_* options must be set to id's
elem_companyname or id of the input element, can be blank
elem_house_numname or id of the input element, can be blank
elem_street1name or id of the input element, required, must be set
elem_street2name or id of the input element, can be blank
elem_street3name or id of the input element, can be blank
elem_townname or id of the input element, required, must be set
elem_countyname or id of the input element, can be blank
elem_postcodename or id of the input element, required, must be set; this is both an input and an output
traditional_countywhich flavour of county info is shown 0 - postal county (default), 1 - traditional county name
busy_img_urlset the location of a busy image, default is 'crafty_postcode_busy.gif' - this is shown while we wait for the server response
max_widthset if you want to limit the width of the result box (in px), eg "450px"
max_linesset the max number of text lines in the result box
hide_result1 - results box disappears once a result is clicked, 0 - it stays up (default)
org_uppercase0 - company name with leading upper case, 1- all in upper case (default)
town_uppercase0 - leading upper case, 1- all in upper case (default, recommended by Royal Mail)
county_uppercase0 - leading upper case (default), 1- all in upper case
addr_uppercase0 - rest of address lines with leading upper case (default), 1- all in upper case
delimiterdelimiter to use between parts of the address, eg ", " (default)
msg1msg to attach as title to busy image, default - 'Please wait while we find the address'
err_msg1error msg if postcode does not exist, default - 'This postcode could not be found, please try again or enter your address manually'
err_msg2error msg if postcode is not correctly formatted, default - 'This postcode is not valid, please try again or enter your address manually'
err_msg3error msg if there is network problem, default - 'Unable to connect to address lookup server, please enter your address manually.'
err_msg4error msg to cover any other problem, default - (err_num)+'An unexpected error occurred, please enter your address manually.'
res_autoselect1 - the first result will be auto-selected as soon as the result box is shown (default), 0 - it won't
res_select_on_change1 - results will be selected as the user scrolls through the box (default), 0 - user must explicitly click to select
first_res_lineoption to add a dummy 1st line eg '----- please select your address ----', blank by default
debug_modeset it to 1 to get more descriptive error messages in case of trouble, default is 0
lookup_timeoutmaximum time to spend waiting for lookup server response (in milliseconds), default 10000, i.e. 10 seconds.
on_result_readycallback function name called when result is received from the lookup server
on_result_selectedcallback function name called when the user clicks on a result
on_errorcallback function called if there is an error
pre_populate_common_address_parts1 - every time the drop-down is shown all common parts of the address get placed on the form, default is 0
lookup_urlset to use a data relay script, by default set to access the CraftyClicks web service directly
basic_address0 - Rapid/Flexi Address, 1 - Basic Address
single_res_autoselect1 - if only one result is found, we select it right away rather than show a one line drop down!
single_res_noticeif single_res_autoselect = 1, show this message if drop down in not shown

Configuring the address form

There are two methods to tell the object where to find the address form fields:

  • Tell the script the name of the form and the names of the fields.
  • Attach unique IDs to all the fields.

Method 1:

<script>
  cp_obj.set("form"          , "XYZ");
  cp_obj.set("elem_company"  , "companyname");
  cp_obj.set("elem_street1"  , "address1");
  //... etc ...
</script>

Method 2:

Add id="*_id" to every <input> field.

<script>
  cp_obj.set("form", "");
  cp_obj.set("elem_company"  , "companyname_id");
  cp_obj.set("elem_street1"  , "address1_id");
  //... etc ...
</script>

Minimum configuration:

Not all address fields need to be set, the minimum set would be:

  cp_obj.set("elem_company"  , "");
  cp_obj.set("elem_street1"  , "address1");
  cp_obj.set("elem_street2"  , "");
  cp_obj.set("elem_street3"  , "");
  cp_obj.set("elem_town"     , "town");
  cp_obj.set("elem_county"   , "");
  cp_obj.set("elem_postcode" , "postcode");
  cp_obj.set("elem_house_num", ""); 

Debugging

If things don't work right away, set the object into debug mode and see if the error messages give any clues:

  cp_obj.set("debug_mode",   "1");

If you still have no luck - email any error codes/messages to us. We will be happy to help.