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 799389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:07:07+00:00 2026-05-14T23:07:07+00:00

I am creating a site that requires a very small postcode checker. I have

  • 0

I am creating a site that requires a very small postcode checker. I have approx 8 postcode prefix’s, HX, HD, BD, LS etc in an array. I also have a simple input field and submit btn. When the user types in a postcode for example HX5 9DU I want Jquery to check the array, if there is match for the first 2/3 letters I want a div to fade in displaying a message.

How would I do this? Many thanks in advance.

  • 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-05-14T23:07:07+00:00Added an answer on May 14, 2026 at 11:07 pm

    Here’s one way to do it:

    http://jsfiddle.net/uNKhs/3/

    HTML:

    <div id="message">some message</div>
    
    <input type="text" id="myInput" />​
    

    jQuery:

    $(function() {  // Wrap the code such that it will run after document loads
    
        $("#message").hide();   // First hide the message
    
        var prefix = ['HX', 'HD', 'BD', 'LS'];   // Create your array of prefixes
    
        $('#myInput').keyup(function(e) {   // Trigger an event handler on each 'keyup'
    
            var value = $(this).val();   // Grab the current value of the input
    
                // Store first two characters in a variable, and convert to upper case
            var firstTwo = value.substr(0,2).toUpperCase();
    
    
                 // Check that the length of the input is greater than one,
                 //    and that the firstTwo characters are in the prefix array
            if(value.length > 1 && ($.inArray(firstTwo, prefix) >= 0)) {
    
                    // If so, find the hidden element with ID "message" and fade it in.
                $("#message:hidden").fadeIn();
            }
        });
    
    });
    

    Some related jQuery docs:

    .hide() http://api.jquery.com/hide/

    $.inArray() http://api.jquery.com/jQuery.inArray/

    .fadeIn() http://api.jquery.com/fadein/

    .keyup() http://api.jquery.com/keyup/

    .val() http://api.jquery.com/val/


    EDIT:

    When running jQuery code, it is usually best to have your code run after the document has loaded. You can do this a couple different ways.

    $(document).ready(function() {
        // My jQuery code
    });
    

    or

    $(function() {
        // My jQuery code
    });
    

    The two will accomplish the same thing.

    I updated my answer to include the second version.


    BONUS:

    This version will update the input with the upper case version if the user types lower case characters for the first two characters.

    EDIT: Also, it shows a fail message if a match in the array is not found.

    http://jsfiddle.net/uNKhs/8/

    $(function() {
        $("#message").hide();
        $("#fail").hide();
    
        var prefix = ['HX', 'HD', 'BD', 'LS']
    
        $('#myInput').keyup(function(e) {
            var value = $(this).val();
            var firstTwo = value.substr(0,2);
            var firstTwoUpper = firstTwo.toUpperCase();
    
            if(firstTwo != firstTwoUpper) {
                $(this).val( value.replace(/^\w\w/, firstTwoUpper) );
            }
            if(value.length > 1) {
                if($.inArray(firstTwoUpper, prefix) >= 0) {
                    $("#fail").hide();
                    $("#message:hidden").fadeIn();
                } else {
                    $("#message").hide();
                    $("#fail:hidden").fadeIn();
                }
            } else {
                $("#message").hide();
                $("#fail").hide();
            }
        });
    });
    ​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following entities. User Event Expense Guest A User is someone that
I am creating a web-page/website that integrates all my accounts into one spectrum, as
This is more or less a best-practice question: I'm working on a site that
I have been given the task to fix an Embedded Operating System which is
I am seeing a very strange problem on one of my production boxes. We
What is the best method for applying drop shadows? I'm working on a site
Ok, let me try to clearly explain what I'm attempting to accomplish here. Basically,
The more I use ASP.NET MVC, the more I love it. However, in the
My company has been evaluating Spring MVC to determine if we should use it

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.