jQuery Plugin Demonstration

Try our jQuery plugin demonstration
Take a peek under the hood with our simple HTML & Javascript examples

<!-- Add a div to house your 
     postcode input field -->
<div id="lookup_field"></div>

<!-- This is your existing form -->
<label>Address Line One</label>
<input id="first_line" type="text">

<label>Address Line Two</label>
<input id="second_line" type="text">

<label>Address Line Three</label>
<input id="third_line" type="text">

<label>Post Town</label>
<input id="post_town" type="text">

<label>Postcode</label>
<input id="postcode" type="text">
<script>
// Add this after your form
$('#lookup_field').setupPostcodeLookup({
  // Add your API key
  api_key: 'ak_hk71kco54zGSgVf9ExxRVVNmolLNh',
  // Identify your fields with CSS selectors
  output_fields: {
    line_1: '#first_line',  
    line_2: '#second_line',         
    line_3: '#third_line',
    post_town: '#post_town',
    postcode: '#postcode'
  }
});
</script>

Get up and running in 3 steps

2

Include the plugin script on page with a form

3

Point the plugin to your address fields

Find the jQuery Plugin on Github.

Getting started

In a typical address form, you have an existing set of address fields you wish to populate with accurate user data.

To use the plugin, load the script, pass in your API Key and CSS selectors to identify your existing address fields.

As long as the CSS selector identifies the relevant field uniquely, it will not matter if the selector references the element's ID, class or name.


Play with this example

$('#lookup_field').setupPostcodeLookup({
  api_key: 'iddqd',
  output_fields: {
    line_1: '#first_line',  
    line_2: '#second_line',         
    line_3: '#third_line',
    post_town: '#post_town',
    postcode: '#postcode'
  }
});

//$.fn.setupPostcodeLookup() creates the necessary 
// lookup elements (input field and lookup button) 
// in specified element (in this case, the empty div 
// tag with id='lookup_field'
<div id="lookup_field"></div> 
<!-- The above empty div tag will allow the 
        plugin to create the lookup fields -->

<!-- Below are your existing input fields -->
<label>Address Line One</label> 
<input id="first_line" type="text">

<label>Address Line Two</label>
<input id="second_line" type="text">

<label>Address Line Three</label>
<input id="third_line" type="text">

<label>Post Town</label>
<input id="post_town" type="text">

<label>Postcode</label>
<input id="postcode" type="text">

Pulling in additional data

We can also pull in more data from the Postcode Address File by passing more arguments into the "output_fields" object.

In this instance, we pull location, organisation name (if any) and the Unique Delivery Point Reference Number.

The full list of available data fields can be found here.

Try a postcode with lots of local businesses like NW1 0BG.


Play with this example

$('#lookup_field').setupPostcodeLookup({
    api_key: 'iddqd',
    output_fields: {
        line_1: '#first_line',
        line_2: '#second_line',
        line_3: '#third_line',
        post_town: '#post_town',
        postcode: '#postcode',
        organisation_name: '#organisation_name',
        longitude: '#longitude',
        latitude: '#latitude',
        udprn: '#udprn'
    }
});

// Note the new properties passed into output_fields 
// along with their associated CSS selectors
<div id="lookup_field"></div>

<!-- Let's add some new input fields below to house 
        the additional data points -->

<label>Organisation Name</label>
<input id="organisation_name" type="text">
<label>Longitude</label>
<input id="longitude" type="text">
<label>Latitude</label>
<input id="latitude" type="text">
<label>Unique Delivery Point Reference Number</label>
<input id="udprn" type="text">
    
    
<label>Address Line One</label>
<input id="first_line" type="text">
<label>Address Line Two</label>
<input id="second_line" type="text">
<label>Address Line Three</label>
<input id="third_line" type="text">
<label>Post Town</label>
<input id="post_town" type="text">
<label>Postcode</label>
<input id="postcode" type="text">

Styling with CSS & classes

The input field and buttons can be styled by declaring classes in the configuration object.

In this example the input and button classes have been set to my-input-class and my-button-class and styled using CSS.


Play with this example

$('#lookup_field').setupPostcodeLookup({
    api_key: 'iddqd',
    output_fields: {
        line_1: '#first_line',
        line_2: '#second_line',
        line_3: '#third_line',
        post_town: '#post_town',
        postcode: '#postcode'
    },
    button_class: 'my-button-class',
    input_class: 'my-input-class'
});
<div id="lookup_field"></div>

<label>Address Line One</label>
<input id="first_line" type="text">

<label>Address Line Two</label>
<input id="second_line" type="text">

<label>Address Line Three</label>
<input id="third_line" type="text">

<label>Post Town</label>
<input id="post_town" type="text">

<label>Postcode</label>
<input id="postcode" type="text">

Adding multiple lookups on a page

You can create multiple, independent postcode lookup fields on the same page. This is useful for certain requirements. E.g. separate billing and shipping addresses.

.setupPostcodeLookup() can be invoked multiple times on different DOM elements to create multiple fields.

Each lookup field is independent and so can behave differently if you pass in different configuration settings.


Play with this example

// Hooking up the first lookup field
$('#lookup_field').setupPostcodeLookup({
  api_key: 'iddqd',
  output_fields: {
    line_1: '#first_line',  
    line_2: '#second_line',         
    line_3: '#third_line',
    post_town: '#post_town',
    postcode: '#postcode'
  }
});

// Hooking up the second lookup field
$('#lookup_field_2').setupPostcodeLookup({
  api_key: 'iddqd',
  output_fields: {
    line_1: '#first_line_2',  
    line_2: '#second_line_2',         
    line_3: '#third_line_2',
    post_town: '#post_town_2',
    postcode: '#postcode_2'
  }
});
<!-- First Lookup Field -->
<div class="box">
    <h2>Lookup Field 1</h2>
    <div id="lookup_field"></div> 

    <label>Address Line One</label> 
    <input id="first_line" type="text">

    <label>Address Line Two</label>
    <input id="second_line" type="text">

    <label>Address Line Three</label>
    <input id="third_line" type="text">

    <label>Post Town</label>
    <input id="post_town" type="text">

    <label>Postcode</label>
    <input id="postcode" type="text">
</div>
        
<!-- Second Lookup Field -->
<div class="box">
    <h2>Lookup Field 2</h2>
    <div id="lookup_field_2"></div> 

    <label>Address Line One</label> 
    <input id="first_line_2" type="text">

    <label>Address Line Two</label>
    <input id="second_line_2" type="text">

    <label>Address Line Three</label>
    <input id="third_line_2" type="text">

    <label>Post Town</label>
    <input id="post_town_2" type="text">

    <label>Postcode</label>
    <input id="postcode_2" type="text">
</div>

Pulling in your own input field

You may not want the plugin to create its own postcode input field. Instead, you may wish to use an existing field.

The plugin allows you to define a pre-existing element to accept inputs for your postcode lookup. The only proviso is that the element is able to respond to $.fn.val(). For instance <input>, <textarea> or <select> would suffice.

To enable this behaviour simply identify your custom input with CSS selectors, via the input property.


Play with this example

$('#lookup_field').setupPostcodeLookup({
  api_key: 'iddqd',
  output_fields: {
    line_1: '#first_line',  
    line_2: '#second_line',         
    line_3: '#third_line',
    post_town: '#post_town',
    postcode: '#postcode'
  },
  input: '#customInput'
});

//Define an DOM element which you wish to serve as the input to the postcode lookup. This element will need to respond to $.fn.val in order to work. In otherwords, it should be an input, select or textarea.
<!-- Below is the custom input field, we're using a textarea in this example -->

<textarea id="customInput" type="text" value="This is my custom input element">
This is my custom input element. It can be an input, select or textarea element.
</textarea>

<div id="lookup_field"></div>

<label>Address Line One</label> 
<input id="first_line" type="text">

<label>Address Line Two</label>
<input id="second_line" type="text">

<label>Address Line Three</label>
<input id="third_line" type="text">

<label>Post Town</label>
<input id="post_town" type="text">

<label>Postcode</label>
<input id="postcode" type="text">

Applying your own button

You may also specify any clickable DOM element as a trigger to lookup a postcode.

Simply identify your custom input via the button property using CSS selectors.


Play with this example

$('#lookup_field').setupPostcodeLookup({
  api_key: 'iddqd',
  output_fields: {
    line_1: '#first_line',  
    line_2: '#second_line',         
    line_3: '#third_line',
    post_town: '#post_town',
    postcode: '#postcode'
  },
  button: '#customButton'
});

//Define the button parameter with the id of your custom DOM element. This element will trigger a postcode lookup when clicked.
<div id="lookup_field"></div>

<!-- Here we've created a link to be used as a trigger for a postcode lookup -->

<h3><a href="" id="customButton">My Custom Lookup Link</a></h3>

<label>Address Line One</label> 
<input id="first_line" type="text">

<label>Address Line Two</label>
<input id="second_line" type="text">

<label>Address Line Three</label>
<input id="third_line" type="text">

<label>Post Town</label>
<input id="post_town" type="text">

<label>Postcode</label>
<input id="postcode" type="text">

Omitting organisation name from address lines

If an organisation is registered at an address, clear addressing guidelines indicate that the organisation name should appear on the first line of the address (line_1).

This may not be ideal for all use cases however. E.g. you may want to collect the organisation name in a separate field.

By setting remove_organisation to true, the plugin will remove any organisation name from address lines.


Play with this example

$('#lookup_field').setupPostcodeLookup({
  api_key: 'iddqd',
  remove_organisation: true,
  output_fields: {
    organisation_name: "#organisation",
    line_1: '#first_line',  
    line_2: '#second_line',         
    line_3: '#third_line',
    post_town: '#post_town',
    postcode: '#postcode'
  }    
});
<div id="lookup_field"></div> 

<label>Organisation</label> 
<input id="organisation" type="text">

<label>Address Line One</label> 
<input id="first_line" type="text">

<label>Address Line Two</label>
<input id="second_line" type="text"> 

<label>Address Line Three</label>
<input id="third_line" type="text">

<label>Post Town</label>
<input id="post_town" type="text">

<label>Postcode</label>
<input id="postcode" type="text">

Checking the usability of your key

For some applications, you may expect your key to not be usable in certain situations. For instance,

  • You may not have balance on your key for some reason.
  • You've set a daily lookup limit on your key to save money.
  • Your user may have exceeded their daily limit set by you.

You can instruct the plugin to make provisions in case your key can't be used right there and then.

By setting check_key to true the plugin will check if the key is usable when it is loaded on a page. If the check fails, the plugin will not initialise.

You can also use the onLoaded and onFailedCheck callbacks to make further changes to your page depending on whether your key can be used.


Play with this example

$('#lookup_field').setupPostcodeLookup({
  // Test key: "iddqd" passes the check
  api_key: 'iddqd',
  
  // Test key: "idkfa" fails the key check
  // api_key: 'idkfa',
   
  check_key: true, 
    
  // Invoked if plugin loads
  onLoaded: function () {
      $("#resultCheck").html("Try using our test postcode: ID1 1QD");
  },
  
  // Invoked if key fails check
  onFailedCheck: function () {
      $("#resultCheck").html("Please manually enter your address");
  },

  output_fields: {
    line_1: '#first_line',  
    line_2: '#second_line',         
    line_3: '#third_line',
    post_town: '#post_town',
    postcode: '#postcode'
  }    
});
<p id="resultCheck"></p>

<div id="lookup_field"></div> 

<label>Address Line One</label> 
<input id="first_line" type="text">

<label>Address Line Two</label>
<input id="second_line" type="text"> 

<label>Address Line Three</label>
<input id="third_line" type="text">

<label>Post Town</label>
<input id="post_town" type="text">

<label>Postcode</label>
<input id="postcode" type="text">
   

Enable Full Address Search

You can also enable a full address search in case the postcode is partially or completely unknown.

Set address_search to true and the plugin will perform a full address search if the search term is not a valid postcode.


Play with this example

<script>
$('#lookup_field').setupPostcodeLookup({
  api_key: 'ak_hk71kco54zGSgVf9ExxRVVNmolLNh',
  // Enable address search
  address_search: true,
  // Add a custom label
  input_label: "Search for a postcode or address",
  output_fields: {
    line_1: '#first_line',  
    line_2: '#second_line',         
    line_3: '#third_line',
    post_town: '#post_town',
    postcode: '#postcode'
  }
});
</script>
<div id="lookup_field"></div> 

<label>Address Line One</label> 
<input id="first_line" type="text">

<label>Address Line Two</label>
<input id="second_line" type="text">

<label>Address Line Three</label>
<input id="third_line" type="text">

<label>Post Town</label>
<input id="post_town" type="text">

<label>Postcode</label>
<input id="postcode" type="text">

Run a custom callback upon API request

By passing a function to onLookupSuccess, you can execute a custom callback. onLookupSuccess accepts a data argument which represents the raw response body for your request.

This option is useful if you want to run custom code after the user has searched for an address and before the user selects from the dropdown menu.


Play with this example

$('#lookup_field').setupPostcodeLookup({
  api_key: 'iddqd',
  output_fields: {
    line_1: '#first_line',  
    line_2: '#second_line',         
    line_3: '#third_line',
    post_town: '#post_town',
    postcode: '#postcode'
  },
  onSearchCompleted: function (data) {
    if (data.code === 2000) {
      // Success
      $("#successStatus")
        .html("Success!"+ JSON.stringify(data,2,2));
    } else if (data.code === 4040) {
      // Postcode does not exist
      $("#successStatus")
        .html("Postcode does not exist!");
    } else {
      $("#successStatus")
        .html("Some error occurred");
    }
  }
});
<pre id="successStatus"></pre>

<div id="lookup_field"></div> 

<label>Address Line One</label> 
<input id="first_line" type="text">

<label>Address Line Two</label>
<input id="second_line" type="text">

<label>Address Line Three</label>
<input id="third_line" type="text">

<label>Post Town</label>
<input id="post_town" type="text">

<label>Postcode</label>
<input id="postcode" type="text">

Need help? If you have any questions or need help debugging, come talk to us