Skip to content
Advertisement

Handle multiple substrings in Woocommerce validation checkout form

a few years ago someone added code to our website (WordPress/Woocommerce Checkout), which validates a field against a prefix and field length.

The field validation code looks like this:

JavaScript

It validates against MA- and an allover length of 9 letters. For example, MA-123123 is OK, MA-123 isn’t.

Now we want to validate against MA-xxxxxx and BW-xxxxxx but I’m not sure how the correct syntax is.

I tried already the following:

JavaScript

But with this the validation fails again both, MA- and BW-.

Can someone help me with this?

Advertisement

Answer

The following using in_array() will handle both substrings as follow:

JavaScript

or using preg_match() too this way:

JavaScript

Tested and works.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement