Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8276711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:27:08+00:00 2026-06-08T08:27:08+00:00

I need to write some JavaScript functionality that analyses an address block and displays

  • 0

I need to write some JavaScript functionality that analyses an address block and displays an error
message if the postcode in this delivery address is within one of a given list (eg. going to be affected by
the Olympics). I have used indexOf to match the contents of the address block against a list
of affected postcodes and this works fine.

However the problem occurs because a postcode starting with DW3 which is not affected will
display the affected message because the indexOf is picking up W3. As you can see in my
code below i have tried to match on a space preceeding the postcode
i.e. ” DW3″ but this doesn’t work at all and i have no idea why.

Perhaps regular expressions is a better way to match this pattern, unfortunately
I am terrible at regex, so can anyone help with this?

An example address would be:

<address>Mr Jim Smith<br>
    Flat, 4 Spring Lane<br>
    EASTLEIGH<br>
    DW3 6LS<br>
    United Kingdom</address>

here is the JavaScript:

var AFFECTED_POSTCODES = ["DT1","DT10","DT11","DT2","DT3","DT4","DT5","DT6","DT8","DT9","E1","E10","E11","E13","E14","E15","E16","E18","E1W","E3","E6","E9","EC1A","EC2N","EC2R","EC2V","EC2Y","EC3M","EC3N","EC3R","EC3V","EC4","EC4M","EC4N","EC4R","EC4V","EC4Y","GU22","GU23","GU4","GU5","HA9","IG1","IG11","IG4","IG7","IG8","KT1","KT10","KT11","KT12","KT13","KT14","KT18","KT2","KT20","KT22","KT24","KT6","KT7","KT8","NW1","NW10","NW8","RH4","RH5","RM13","RM15","RM9","SE1","SE10","SE11","SE18","SE3","SE7","SL0","SL3","SW10","SW11","SW13","SW15","SW18","SW19","SW1A","SW1E","SW1H","SW1P","SW1V","SW1W","SW1X","SW1Y","SW3","SW5","SW6","SW7","SW8","TW1","TW10","TW11","TW19","TW5","TW6","TW7","TW8","TW9","UB3","UB7","W10","W11","W12","W14","W1B","W1C","W1G","W1H","W1J","W1K","W1T","W1U","W1W","W2","W3","W4","W5","W6","W7","W9","WC1A","WC1B","WC1E","WC1H","WC1N","WC1R","WC1V","WC2A","WC2B","WC2E","WC2N","WC2R"];
function checkPostcode(e){
    var isAffected = false,
        field = $('#delivery_details_wrapper address'),
        fieldText = $(field).text();
    for (var i=0; i<AFFECTED_POSTCODES.length; i++){
        var patt = ' ' + AFFECTED_POSTCODES[i];
        if (fieldText.indexOf(patt) !== -1) {
            isAffected = true;
        }
    }
    if (isAffected) {
        if ($('.warning').length === 0) {
            field.after('<p class="warning" style="margin-top: 10px; clear: both;">* Delivery to this postcode may be affected by the Olympics. For more info click <a href="'+ Arcadia.Loader.rootPath +'lib/html/olympic_message.html" class="lightbox" rel="lightbox({innerWidth: 600, innerHeight: 300})" title="Click here for postcodes that may be affected by the olympics">here</a></p>');
        }
    } else {
        $('.warning').remove();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T08:27:10+00:00Added an answer on June 8, 2026 at 8:27 am

    A regular expression would indeed be better, being more concise and more efficient (the expression will compile to an optimal matching automaton). It would also be quite simple.

    var alternation = AFFECTED_POSTCODES.join('|');
    var r = new RegExp('\\b(' + alternation + ')\\b');
    var isAffected = r.test(fieldText);
    

    Here the \b means match a word boundary, which will deal with your sub-string problem.
    You may even just write out the postcodes in a regex literal – /\b(DT1|DT10...)\b/ – if you don’t need the array anywhere else.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write some client-side javascript for a webpage that will truncate the
I need to create a function in JavaScript, that will write some text (which
Hallo, i need to write some javascript that gets the contents of a userfield
Imagine that you need to write some Javascript that simply changes a set of
I need to write some code that would loop though all rows of a
I need to write some code (in any language) to process 10,000 files that
Maybe I am just over thinking this and need to write some more prototype
I need to write some Javascript to check an input to only allow number
I'm trying to write some simple chat client in javascript, but I need a
I need to do a function on Jquery that write in a div some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.