2

I've been working on a postcode checker which I need to have a partial match on the array. ie: if the user current uses L20 it shows true but if the uses L20 1WE if shows false.

I need it to read the array and if any part of the user input matches it shows true.

I have a demo up here: https://codepen.io/paulmaloney/pen/dfa0603f200a8f5be89b0a10d7ba80f6

var postcodes = ["PR8","PR9","WA11","L1","L2","L20","L80"];

$('#searchForm').submit(function(){
    var postcode = $('#searchForm input').val();  
    if($.inArray(postcode.toUpperCase(), postcodes ) > -1){
        $('#result').html('Yes, we cover your area!');
    }else{
        $('#result').html('Sorry, it looks like we do not cover that area yet.');
    }

    return false;
});

That's my code thus far. I know I'm missing something silly but can't work it out

4
  • L20 1WE ? I can't understand this part: if the user current uses L20 it shows true but if the uses L20 1WE if shows false Commented Feb 2, 2020 at 10:33
  • 1
    L20 1WE if shows false must be true as he wants partial matches Commented Feb 2, 2020 at 10:35
  • It doesn't partial match, it's only working for an exact match currently. Commented Feb 2, 2020 at 10:35
  • 1
    What do you mean by part ? Is it a sequence of characters between spaces or the string's start and end ? Commented Feb 2, 2020 at 10:38

2 Answers 2

1

You can split the input then use some() and includes() like the following way:

var postcodes = ["PR8","PR9","WA11","L1","L2","L20","L80"];

$('#searchForm').submit(function(){
    var postcode = $('#searchForm input').val().toUpperCase();
    postcode = postcode.trim().includes(' ') ? postcode.split(' ') : postcode.match(/.{1,3}/g);
    if(postcode.some(i => postcodes.includes(i))){
        $('#result').html('Yes, we cover your area!');
    }else{
        $('#result').html('Sorry, it looks like we do not cover that area yet.');
    }

    return false;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form role="search" id="searchForm">
  <input type="text" name="s" class="form-control covered-area-search-input" placeholder="Enter the first half of your postcode...">
  <input type="submit" value="Search"/> 
</form>
<div id="result"></div>

Maybe, splitting and matching do not required. Simply checking if the current value includes any item of the array.

var postcodes = ["PR8","PR9","WA11","L1","L2","L20","L80"];

$('#searchForm').submit(function(){
    var postcode = $('#searchForm input').val().toUpperCase();
    if(postcodes.some(i => postcode.includes(i))){
        $('#result').html('Yes, we cover your area!');
    }else{
        $('#result').html('Sorry, it looks like we do not cover that area yet.');
    }

    return false;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form role="search" id="searchForm">
  <input type="text" name="s" class="form-control covered-area-search-input" placeholder="Enter the first half of your postcode...">
  <input type="submit" value="Search"/> 
</form>
<div id="result"></div>

Sign up to request clarification or add additional context in comments.

6 Comments

Awesome that's almost it! It still shows false if the user doesn't add a space ie: L201WE.
@Paul, if there is no space then you can chuck the input with 3 characters, updated the answer, please check:)
This will still fail is the input is something like L1ABC. It should match because L1 (two characters not three) is in the array.
You can use something like postcodes.some(c => userInput.contains(c)) (and maybe lowercase both c and userInput).
I've meant to say .includes(..) (as in String.prototype.includes()) or maybe .startsWith(..) will be better, that depends on what the OP wants, it is not clear what he means by part.
|
1

$.inArray does a strict check between postcode and values in postcodes array.

Since you're checking that postcode entered by user is prefixed by any postcode in postcodes array you need to write a custom filter for that.

Find every matched postcode in postcode array to the first part of the post code entry.

postcodePart = postcode.toUppercase().split(' ').shift()
matches = postcodes.filter(
  postcode => new RegExp("^" + postcode + "$").test(postcodePart)
)

Check the length of the matched postcode and verify that there is a match.

if (matches.length == 1) {
   $('#result').html('Yes, we cover your area!');
} else {
   $('#result').html('Sorry, it looks like we do not cover that area yet.');
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.