0

The website is for a business that only provides services in certain zip codes. I would like to be able to display different messages based on if the zip code matches one in my list or not.

I couldn't find any plugins that do this that do not require WooCommerce.

2
  • So you're just validating a form field against a list you've hardcoded or set in an option? That should be easy enough to do with just form validation. How have you set up the form? Is it your own code or a forms plugin?
    – Rup
    Feb 13 at 14:27
  • if you're asking for a plugin recommendation then we'd have to close the question as offtopic as that's not allowed here, however, if you have a list of the postcodes your business supports then this should be straightforward, stop thinking of it as an availability checker, and instead simplify the problem to "how do I check that something a user put in a text box appears in this array of strings?" where the strings just happen to be zip codes, and coincidentally the text box has the label "zipcode checker"
    – Tom J Nowell
    Feb 13 at 14:58

1 Answer 1

1

This is much simpler version of your question:

Given a list of strings, how do I check the user input is in that list?

You don't need a special service or fancy software to do that, just make a big list of the zipcodes you support and use in_array.


const $supported_zipcodes = [
    'zipcode1...',
    'zipcode2...',
    ... etc
];

function ncblender_check_zipcode( string $zipcode ) : bool {
    // clean up the zip code, remove trailing spaces and make it all lowercase
    $zipcode = trim( strtolower( $zipcode ) );

    // don't do empty zipcodes
    if ( empty( $zipcode ) ) {
        return false;
    }

    // if our cleaned up zipcode is in the list, return true
    return in_array( $zipcode, $supported_zipcodes, false )
}

Now you can create a generic form with a text input, and use that function to check if it's a supported zipcode or not. You might want to find better zipcode validation code online as what I put above is a simple/crude check, but that might just be enough for you

Not the answer you're looking for? Browse other questions tagged or ask your own question.