raj
raj

Reputation: 15

regex in jquery

I need to provide the P.O Box validation for the address Fields 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:

    "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);
}, "");

The new code designed is partially working like its taking

Polo road 
Testbintest 

But its throwing an error for

Robin road
Robox road 

The below is the new code which is partally working

jQuery.validator.addMethod("nopobox", function(value, element) {
     return ! /(?:p(?:ost)?\.?\s?[o|0](?:\.|ffice)?)\b|(?:b(?:[o|0]x)|(?:in))\b/i.test(value);
}, "PO Boxes are not allowed.");

Upvotes: 0

Views: 824

Answers (3)

reinierpost
reinierpost

Reputation: 8611

If you really want to check against a list of words, it's more maintainable to run a list of tests, each for a particular word.

Upvotes: 0

dala
dala

Reputation: 2025

Have you checked my comment in your old question?

The proper behavior would have been to update the original question with information of your own progress instead of re-posting.

Patience is a virtue.


This is the reply that I wrote in your original question:

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.

Upvotes: 2

chobo2
chobo2

Reputation: 85875

Sorry I am not following. So your looking at what the address line and if it has "PO" as the first letters then it should fail on these?

"PO BOX", "PO BIN", "BIN", "P.O BOX", "P.O BIN", "P.O", "PO"

I am not getting why your looking at "polo Rd" and somehow this seems to decide what your going to let through or not let through.

Then on the flip side you say that these are valid

But "Polo Rd", "Robin Rd", "testbintest" should be accepted as valid address in both the address lines.

But at first you said "Polo Rd" is not valid.

Then I don't get your other validation tests like I don't see why you user can't have this

"Testbintest"

Like whats wrong with it. I thought you just are checking for like P.O box addresses since you don't ship to them or something like that.

I am lost

Your further down post says you just want check for just those words. You can try this

(\s)*P(\s)*.?(\s)*O((\s)*B(\s)*[O,I](\s)*[N,X])?

Test with these expressions

PO
P.O
                P. O
               PO
PO BIN
PO BOX
BIN
P.O BOX
P.O BIN
 P O 1234 

You can try this one also.

 ((\s)*[Pp](\s)*(.)?(\s)*[Oo](\s)*)*([Bb](\s)*[OIoi]+(\s)*[NXin])*

Tested with.

P O 1234 
P.O
                P. O
               PO
PO BIN
PO BOX
BIN
P.O BOX
P.O BIN
Robin road
Robox road
MY little poney P.O test
Testbintest
bob
aagc

Just does not get "P O 1234"

The thing that is really making it hard for me is how many can you expect in an address line do you have to check every word?

Upvotes: 0

Related Questions