Search code examples
jqueryjquery-validation-engine

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


Solution

  • upon direction from Mads Hansen's post i hunted down a regex function "inverter" that allowed me to use my existing regex code and filter the opposite:

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

    Final code for "invreted" P.O. Box search that worked for me is as follows:

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