JQuery ValidationEngine and PO Box

I am currently using jQuery and jquery ValidationEngine for the site, and it works like a charm. The problem is that I need to add the ability to check (and deny) PO code addresses. I looked around quite widely and could not find a regular expression that validationengine would use correctly.

I found a regex that works in Javascript:

\b[p]*(ost)*\.*\s*[o|0]*(ffice)*\.*\s*b[o|0]x\b 

but when I move this regular expression from the normal javascript function to the validationengine language file, the regular expression matches everything, even an empty entry in the text box.

regex i added in jquery.validationengine-en.js looks like this:

 "notPoBox": { "regex": /\b[p]*(ost)*\.*\s*[o|0]*(ffice)*\.*\s*b[o|0]x\b/, "alertText": "* PO Box addresses are not allowed for shipping" }, 

and the form element uses the following:

 <input class="validate[custom[notPoBox]] text-input" type="text" id="ship_add1" name="ship_add2" value="" style="width:598px;" /> 

Is there a way to make this regex work within the framework of validationengine and match correctly? I checked that the regex really works on my own javascript inside the page, since I can create a function that will match and warn about matches like this:

 function poChk() { $("[id*='ship_add1']").blur(function() { var pattern = new RegExp('\\b[p]*(ost)*\\.*\\s*[o|0]*(ffice)*\\.*\\s*b[o|0]x\\b', 'i'); if ($("[id*='ship_add1']").val().match(pattern)) { alert('We are unable to ship to a Post Office Box.\nPlease provide a different shipping address.'); return false; } }); 

I also checked it at http://www.regular-expressions.info/javascriptexample.html , which found matches as expected in a wide variety of entries (po box, po box, PO Box, etc.)

Any help would be appreciated.

Silvertiger

+4
source share
2 answers

in the direction from the Mads Hansen post, I pursued a regular expression inverter function that allowed me to use my existing regular expression code and filter the opposite:

 /^((?!foo).)*$/i, 

The final code for the "invreted" PO. The search in the block that worked for me is as follows:

 "notPoBox": { "regex": /^((?!\b[p]*(ost)*\.*\s*[o|0]*(ffice)*\.*\s*b[o|0]x\b).)*$/i, "alertText": "* PO Box addresses are not allowed for shipping" }, 
+1
source

When you define custom validation, regex will validate to make sure the value matches the expression and will generate a validation error if it is not.

As you have determined, notPoBox passes when the value is a PO Box value .

You need to check the opposite of regular expression matching.

This can be done using the function and returning the negative value of the regular expression test() :

 "notPoBox": { "func": function (field, rules, i, options) { var isPoBox = /\b[p]*(ost)*\.*\s*[o|0]*(ffice)*\.*\s*b[o|0]x\b/i; return !isPoBox.test(field.val()); }, "alertText": "* PO Box addresses are not allowed for shipping" }, 

Working example in this jsfiddle

+2
source

All Articles