4

I have looked through some of the older posts and still a little confused with what is happening. I have a shipping form that DOES NOT allow PO Boxes so I am trying to find a validator to look through and make sure that input field doesn't have PO in it. I am making sure every field is filled out with this code but wondering how I could incorporate in the PO box validation with it. Note: this is a separate file from my actual form

$( document ).ready(

    function()
    {
        $( '#shipping' ).submit(
            function()
            {

                    var required_fields = new Array(
                        'name',
                        'service',
                        'company',
                        'contact',
                        'street',
                        'city',
                        'state',
                        'zip',
                        'projectnum'
                    );
                    
                            
                        for( j in required_fields )
                        {
                                            
                            var theRequiredField = required_fields[j]
                            var inputField = $( '[name="' + theRequiredField + '"]' )
                                            
                            if( inputField.val() == '' )
                            {                                   
                                alert( "The '" + theRequiredField + "' field is required." );
                                inputField.focus();
                                return false;
                                    
                            } 

                        } 
                        
                                                                
            } // function
            ) // submit
    }
                
);  
Mukesh
  • 7,630
  • 21
  • 105
  • 159
Mike Jones
  • 579
  • 1
  • 9
  • 15

2 Answers2

9

There may be a better method, but here's what I came up with:

Live Demo

$('input[name=address]').each(function() {
    var pattern = new RegExp('[PO.]*\\s?B(ox)?.*\\d+', 'i');
    if ($(this).val().match(pattern)) {
        $(this).after('<span class="pob">No PO Boxes</span>');
    }
});
drudge
  • 35,471
  • 7
  • 34
  • 45
  • Perfect, thanks for the help. I had to manipulate it a bit with mine but the concept was there and it works. Thank you so much! – Mike Jones Apr 14 '11 at 19:36
  • @Mike: You bet. I've tried to include as many oddities as I could in the JSFiddle demo, so hopefully you won't run into any false positives. – drudge Apr 14 '11 at 19:52
  • This fails, Anything with characters PO and followed by any string with B and numbers would fail. So anyone taking the reference, consider these commonly occurring scenarios. Example: Harpor Bay Apt 212, Portsmouth Blvd, Apt 204 – HarsH Mar 21 '22 at 06:35
1

I used the RegEx provided above by drudge. It works for most cases, but it gave me false positive for strings like "1122 new space – Apt. 99B1". So I modified RegEx to:

var pattern = new RegExp('(P.O.|PO|Box)+\\s?.*\\d+', 'i');

This one checks for either of "P.O." or "PO" or "Box" words, followed by a number to determine it as PO Box address.

Tom Zych
  • 13,329
  • 9
  • 36
  • 53
Mihir Kagrana
  • 479
  • 4
  • 4