Checking PO code in javascript

I am trying to validate a PO field with all conditions met in JavaScript. But it doesn't work for me. it doesn't match any of the cases

below is the reg expression I used

function validatestreetAddress(street){
var streetval=street.value;
var pattern =new RegExp('/^ *((#\d+)|((box|bin)[-. \/\\]?\d+)|(.*p[ \.]? ?(o|0)[-. \/\\]? *-?((box|bin)|b|(#|num)?\d+))|(p(ost)? *(o(ff(ice)?)?)? *((box|bin)|b)? *\d+)|(p *-?\/?(o)? *-?box)|post office box|((box|bin)|b) *(number|num|#)? *\d+|(num|number|#) *\d+)/i');


 if (streetval.match(pattern)) { 
                    alert('We are unable to ship to a Post Office Box.\nPlease provide a different shipping address.'); 
}

      

only one reg exp works for me, but it doesn't meet all the PO box check conditions.

var pattern = new RegExp("\\b[P|p]*(OST|ost)*\\.*\\s*[O|o|0]*(ffice|FFICE)*\\.*\\s*[B|b][O|o|0][X|x]\\b");

      

can anyone suggest what is wrong in reg ex.

I want to match "Box 123", "Box-122", "Box122", "HC73 PO Box 217", "PO Box125", "PO Box", "PO 123", "PO Box 123", "PO Box" , "POB 123", "POB 123", "POB", "POB 123", "POB", "POBOX123", "Po Box", "Post 123", "Post Box 123", "Post" Box Box 123 "," Mailbox "," Box No. 123 "," Box 122 "," Box 123 "," Number 123 "," p box "," po box ", mailbox - user3495160 3 hours ago

do not match = ["Postal Road", "Box Hill", "123 Some Street", "Dispatcher Office", "Pollo St.", "123 box canyon rd", "777 Post Oak Blvd", "PSC 477 Box 396 "," RR 1 Box 1020 "];

+3


source to share


2 answers


You can use the following:

("(post\s*(office)?)?\s*box\s*[#-]?\s*(\d+)?"|
"[\w\s]*?p(ost)?\s*[.-]?\s*o?\s*\.?\s*b?(ox)?\.?\s*(\d+)?"|
"\s*number\s*\d+")

      



See DEMO

0


source


There are many more ways to describe a PO Box address than what you list in the question. The best way to determine if an address is PO Box or not is to run it with the address validation / standardization tool. (The company I work on makes one of these, YAddress .) If the validated and standardized form of your address conjures "PO BOX", you will respond no matter what the original address says. Address checking tools account for thousands of possible typos, misspellings, common abbreviations, alternative formats, and more.



0


source







All Articles