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?

Recommended Answers

All 3 Replies

Member Avatar for 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."; 
}

Thanks guys! You both have helped out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.