• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2092
  • Last Modified:

RegExp for PO Box validation

I am trying to find a one-line solution for using RegExp to validate the syntax of an address field.  I do not want to allow PO Box entries.  Here is what I have been attempting, I just haven't been able to find the right combination. (this is just testing code)

     //var re = new RegExp("(^P\.? *O\.? *) *(BOX)","gi");
     //var re = new RegExp("(^P\.* *O\.*) *(B*O*X*)","gi");
     //var re = new RegExp("^P(\.|(ost)) *O\.* *(B*O*X*)","gi");
     //var re = new RegExp("^P(\.?)(O|(OST)+)(\.?) *(B*O*X*)","gi");
     //var re = new RegExp("^P[.O]S?T? *","gi");
     var re = new RegExp("^p[ \.ob][ bos][ \.bt]","gi");
     var Address1 = "POB #1234";
     var Address2 = "P.O.B. #1234";
     var Address3 = "PO Box #1234";
     var Address4 = "P.O. Box #1234";
     var Address5 = "Post Office #1234";
     var Address6 = "Post Office Box #1234";
     var Address7 = "PO 1234";
     var Address8 = "16661 SW Swank Road";
     var Address9 = "Port Opening #123";
     alert(Address1.match(re));
     alert(Address2.match(re));
     alert(Address3.match(re));
     alert(Address4.match(re));
     alert(Address5.match(re));
     alert(Address6.match(re));
     alert(Address7.match(re));
     alert(Address8.match(re));
     alert(Address9.match(re));
0
baetha
Asked:
baetha
1 Solution
 
AvitarCommented:
You can not do it with one line of code (unless it is really long =).

This is because you have diffrent data to test for the PO box code.

The problem you have is you need to make a function with a filtered array to test the reg exp against. something like the following is needed where there will be multiple variables to test for:

po_filter=[['post','office'],['p.','o.'],['po'],['pob']];

Use the above filtering array data to plug into a loop (something along these lines):

function po_check(strData){
 aFlag=true;
 for(a=0;a<(po_filter.length+1);a++){
  bFlag=new Array;
  for(b=0;b<(po_filter[a].length+1);b++){
    //Use reg expression here isntead if you want =)
    if(strData.indexOf(po_filter[a][b])){bFlag[bFlag.length+1]=false;}else{bFlag[]=true;}
  }
  //test all the flags
  mx="";
  for(c=0;c<(bFlag.length+1);c++){
      mx=mx+"bFlag["+c+"]==false";
      if(c!=bFlag.length+1){mx+="&&";}
  }
  eval('if('+mx+'){aFlag=false;}');
  if(aFlag==false){last;}
 }
 return aFlag;
}

Finnally it would be a good idea saying in big words that "PO boxes are NOT allowed. All addresses containing PO boxes will be ignored!"
0
 
mikkolsoftCommented:
I agree with Avitar that it will be a pretty long if you have more "weird" cases to check. But you can try this if those examples you gave are pretty much all the cases you need to cover:
/((P\.?O\.?(B\.?)?(\s+Box)?)|(Post\s+Office(\s+Box)?))\s*#?[0-9]+/gi

But Avitar you got a pretty neat & robsut script there and pretty easy to manage. I've never thought of that :)

Mikkol
0
 
whammyCommented:
This would still be easier to do with a regex, as you posted, mikkolsoft.

String validation should always be done with a regex, that's what it's for! :-)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
baethaAuthor Commented:
mikkolsoft,

That expression didn't work.  I really wanted to figure out how to decipher and make it work, but I have reverted to using the following because I just couldn't get it and was spending too much time on it.  I think this is safer and easier to read anyway.

if (AddressField.match(/POB/gi) ||
    AddressField.match(/P.O.B./gi) ||
    AddressField.match(/PO Box/gi) ||
    AddressField.match(/P.O. Box/gi) ||
    AddressField.match(/Post Office/gi) ||
    AddressField.match(/Post Office Box/gi)) {
  return true;
}
else {
  return false;
}
0
 
mikkolsoftCommented:
baetha:

That's weird. Try copy and paste this code. I just copied your example with my expression and it works on my side.

<html>
<head>
<script language="JavaScript">
function checkAdd() {
     var re = /((P\.?O\.?(B\.?)?(\s+Box)?)|(Post\s+Office(\s+Box)?))\s*#?[0-9]+/gi;
     var Address1 = "POB #1234";
     var Address2 = "P.O.B. #1234";
     var Address3 = "PO Box #1234";
     var Address4 = "P.O. Box #1234";
     var Address5 = "Post Office #1234";
     var Address6 = "Post Office Box #1234";
     var Address7 = "PO 1234";
     var Address8 = "16661 SW Swank Road";
     var Address9 = "Port Opening #123";
   alert(Address1.match(re));
    alert(Address2.match(re));
    alert(Address3.match(re));
    alert(Address4.match(re));
    alert(Address5.match(re));
    alert(Address6.match(re));
    alert(Address7.match(re));
    alert(Address8.match(re));
    alert(Address9.match(re));
}
</script>
</head>
<body onLoad="checkAdd()">
</body>
</html>

Mikkol
0
 
baethaAuthor Commented:
That worked!  Before I had copied and pasted the line as such: (different syntax)

var re = new RegExp("((P\.?O\.?(B\.?)?(\s+Box)?)|(Post\s+Office(\s+Box)?))\s*#?[0-9]+","gi")

And that didn't work.  Must be something different with the creation syntax.  Anyway, that did it for me.  Now I just have to decipher and learn how to disect it so I understand it better for next time.

Thanks.
0
 
mikkolsoftCommented:
Ah no wonder :)

I think maybe the \. or \s inside the string which causes the problem. Prob. have to substitute it with \\. and \\s. I kind of hate doing these kinds of conversation coz that means next time if I were to use it in other scripting languages I would need to change it back. Anyway, have fun with regexp.

Mikkol
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now