Good afternoon everyone.
I need to write out a simple validation. It needs to validate only if a user enters certain keywords.
Here what I have so far:

if(!preg_match("Indiana, Ohio", $state)) { 
    $errors .= "You have entered the wrong state."; 
}

What would be the correct preg_match function to only validate for Indiana and Ohio?

Member Avatar
LastMitch

@double_cola

What would be the correct preg_match function to only validate for Indiana and Ohio?

You can used the Zip code or State Abbreviations.

I think it's best to used the State Abbreviations because the Zip code will be a lot of numbers. But for Abbreviations it's just 2 letters!

IN - Indiana
OH - Ohio

I would do it simple way without regular expressions which are not very efficient:

$allowed = array('Indiana', 'Ohio');

if(!in_array(allowed)) {
    $errors .= "You have entered the wrong state."; 
}