views:

268

answers:

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 1 and 2 with the values:

  • "PO BOX", "PO BIN", "BIN", "P.O BOX", "P.O BIN", "P.O", "PO"
  • the above values can be in any case
  • 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.
  • But "Polo Rd", "Robin Rd", "testbintest" should be accepted as valid address in both the address lines

the code which is writen for this 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);
}, "");

Please let me know how I can change it

A: 

edit

I played around with this a bit... don't know if this will match 100% of your cases but try this:

<html>
   <head>
      <title>testing...</title>
      <script type="text/javascript" src="scripts/jquery/1.3.1/jquery.min.js"></script>
      <script type="text/javascript" src="scripts/jquery.validate.min.js"></script>
      <script type="text/javascript">
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.");

$(document).ready(function() {$('#test').validate({rules: {address: {nopobox: false, required: true}}})}); 
      </script>
   </head>
   </body>
      <form id="test" action="#">
         <input type="textbox" id="address" class="required nopobox" />
         <input type="submit" />
      </form>
   </body>
</html>

When I run this I get the "PO Boxes are not allowed" error on: PO, po, p.o, p.o., po box, box, bin, etc, etc. But polo road, testboxtest, etc: no warning. One bug: po road throws and error... I'm not sure you can test 100% of the cases in a single Regex.

end edit

Ok... the Regex master don't seem to be online... I'll give it a shot:

Try this regex (?:(?:p(?:[o|0]st)?\.?(?:[o|0](?:ffice)?\.?))|(?:b[o|0]x|bin))(?=\s\d)

It give me the following in the powertoy: (entered as: s/(?:(?:p(?:[o|0]st)?\.?(?:[o|0](?:ffice)?\.?))|(?:b[o|0]x|bin))(?=\s\d)/**NO PO BOXES**/i for testing.

Matches:

  • PO BOX 123
  • PO BIN 1
  • BIN 1
  • P.O BOX 134
  • P.O BIN 12
  • P.O 12
  • P.o. 12
  • PO 123

Does not match:

  • 123 boxing road
  • 123 box road
  • 123 polo road
beggs
Its not working and the basic requirement is polo road should be positive but PO box should throw an error Please respond me soon thanks the code when I am replacng it its trowing syntax errorjQuery.validator.addMethod("nopobox", function(value, element) { return this.optional(element) || ! s/(?:(?:p(?:[o|0]st)?\.?(?:[o|0](?:ffice)?\.?))|(?:b[o|0]x|bin))(?=\s\d)/**NO PO BOXES**/i for testing.;}, "");
raj
I want poland road , robin road to be working but po box or po bin should throw an error can you please help me out with this logic to write the regex please
raj
I've done my best (see edit above) and it seems to work in most cases, someone better as regex/jquery will have to help now.
beggs
HI beggs it looks kool but the robin road is also throwing an error do you have any clue what makes this robin or robox road work in your code?
raj
Sorry it gives a false positive on anything that ends with 'in' because the 'b' is only grouped with the '[o|0]x, `(?:p(?:ost)?\.?\s?[o|0](?:\.|ffice)?\b)|(?:\bb(?:(?:[o|0]x)|(?:in)))\s[\d]+` fixes that. However I an not sure that this will solve all your issues. You need to exhaustively test it. I did a bit of testing, polo road = valid, po = invalid, post office = invalid, box = valid, bin = valid, box 123 = invalid, bin 123 = invalid, robin road = valid, etc...
beggs