jQuery ValidationEngine and PO Box

I am currently using jQuery and the jquery ValidationEngine for a site, and it works like a charm. The issue is that I need to add the ability to check for (and dis-allow) P.O. Box addresses. I have looked around quite extensively and have not been able to find a regex that validationengine will properly use.

I did find a regex that works in Javascript:

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

but when I move that regex from a normal javascript function into the validationengine language file the regex matches on everything, even a blank entry in the text field.

the regex i added to the jquery.validationengine-en.js is as follows:

"notPoBox": {
    "regex": /\b[p]*(ost)*\.*\s*[o|0]*(ffice)*\.*\s*b[o|0]x\b/,
    "alertText": "* P.O. 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 any way I can get this regex to work within the validationengine framework and match properly? I have verified that the regex does indeed work on my own javascript within the page as i can create a function that will match and alert on matches as follows:

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 on a wide variety of entries (p o box, po box, P.O. Box, etc.)

Any help would be appreciated.

SilverTiger

Answers


When you define a custom validation, the regex will test to validate that the value matches the expression and will generate a validation error if it does not.

The way you have it defined, notPoBox passes when the value is a PO Box value.

You need to check for the opposite of the match for your regex.

You could do that by using a function and returning the negated value of a regex 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": "* P.O. Box addresses are not allowed for shipping"
}, 

A working example in this JSFiddle


Need Your Help

Scraping a float value from the DOM

javascript jquery html css

I have the following element on my page:

Import csv file data to populate a Prolog knowledge base

csv prolog swi-prolog

I have a csv file example.csv (link) which contains two columns with header var1 and var2.