Skip to content
Advertisement

PO Box Regular Expression Validation

Here’s my code, but I can’t ever trigger the alert.

JavaScript

Advertisement

Answer

In javascript, you have to escape your slashes:

JavaScript

Also, you could reduce your pattern a bit by using case-insensitive matching:

JavaScript

Note: Your regex also matches on addresses such as:

  • 123 Poor Box Road
  • 123 Harpo Box Street

I would suggest also checking for a number in the string. Perhaps this pattern from a previous answer would be of some help:

JavaScript

(it won’t match on “Post Office” spelled out, or the numeric replacements.. but it’s a start.)

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