tags:

views:

196

answers:

1

I need to provide the P.O Box validation for the address Fields in the registration page. Now we have a regex validation in jquery which has some limitations as follows:

  1. If an address polo Rd is given, it identifies "po" in polo and alerts error message.

So, we should frame a new validation which should not accept address lines with the values:

  1. "PO BOX", "PO BIN", "BIN", "P.O BOX", "P.O BIN", "P.O", "PO"
  2. the above values can be in any case
  3. spaces before, in between and after the above words should also be found and validated. For example: " P O 1234 " should be validated and alert error message.
  4. But "Polo Rd", "Robin Rd", "testbintest" should be accepted as valid address in both the address lines.

The code right now in jquery validation is:

jQuery.validator.addMethod("nopobox", function(value, element) {
   return this.optional(element) || ! /(P(OST)?\.?\s*O(FF(ICE)?)?\.?\s*(?<!(BOX)|(BIN)))|(^[^0-9]*((P(OST)?\.?\s*O(FF(ICE)?)?\.?)|(?<!(BOX)|(BIN))))/i.test(value);
}, "");
A: 

The solution to your problem is that you need to require a white space after the initial P.O matching. That way you will not match addresses that start with Po-. Then you also need to cover the case with just a plane BOX or BIN.

Try something along these lines:

/^\s*((P(OST)?.?\s*O(FF(ICE)?)?.?\s+(B(IN|OX))?)|B(IN|OX))/i

There are many nice tools to make the design of regular expressions a bit easier to overview. One such tool that you can find online is RegExr.

dala