raj
raj

Reputation: 15

validation in jquery

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:

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

Upvotes: 0

Views: 1303

Answers (1)

beggs
beggs

Reputation: 4195

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

Upvotes: 2

Related Questions