0151 355 4555

Drop Down County List

The Ajax Address Validation script by default tries to enter the standard Royal Mail county for an address into the county field. However, counties are not required by Royal Mail for the vast majority of addresses so this field is often left blank. This causes obvious problems if your address form requires the county field to be filled in.

The best recommendation for this situation is to remove the requirement for the county to be filled in, but if you are not in full control of the website this may not be possible. An even more complex situation is when the county field is presented as a drop-down list rather than a text box.

Although most addresses no longer have a "postal county", they still often have a "traditional" or "administrative" county that can be used instead in these situations. The example below demonstrates overriding the selectAddress method in the Ajax Address Validation script to select the first county that is available for an address and selecting that value from a drop-down list.

// Define your own yourco.selectCountyPostcodeLookupButton class,
// inheriting from data8.postcodeLookupButton

if (typeof (yourco) == 'undefined') {
	yourco = function () { };

yourco.selectCountyPostcodeLookupButton = function (fields, options) {
	data8.postcodeLookupButton.apply(this, arguments);

yourco.selectCountyPostcodeLookupButton.prototype = new data8.postcodeLookupButton();

// Override the selectAddress method to take any available county information
// and try to identify a matching entry from the county drop down list.

yourco.selectCountyPostcodeLookupButton.prototype.selectAddress = function (address) {
	data8.postcodeLookupButton.prototype.selectAddress.call(this, address);

	for (var i = 0; i < this.fields.length; i++) {
		if (this.fields[i].field == 'county') {
			var county = address.RawAddress.PostalCounty;
			if (!county)
				county = address.RawAddress.TraditionalCounty;
			if (!county)
				county = address.RawAddress.AdministrativeCounty;

			if (county) {
				county = this.toProperCase(county);

				var target = this.getElement(this.fields[i].element);
				for (var j = 0; j < target.options.length; j++) {
					if (target.options[j].value == county) {
						target.selectedIndex = j;

// Set up the postcode lookup system for your form.

new yourco.selectCountyPostcodeLookupButton(
		{ element: 'company', field: 'organisation' },
		{ element: 'address1', field: 'line1' },
		{ element: 'address2', field: 'line2' },
		{ element: 'address3', field: 'line3' },
		{ element: 'city', field: 'town' },
		{ element: 'county', field: 'county' },
		{ element: 'postcode', field: 'postcode' }
		// Change this to your own Ajax key
		ajaxKey: 'your-ajax-key',

		// Change this to your postcode lookup license type
		license: 'WebClickFull'