?
Solved

RegExp for PO Box validation

Posted on 2003-03-18
7
Medium Priority
?
1,990 Views
Last Modified: 2008-02-01
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
Comment
Question by:baetha
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 

Expert Comment

by:Avitar
ID: 8161906
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
 
LVL 2

Expert Comment

by:mikkolsoft
ID: 8162096
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
 
LVL 4

Expert Comment

by:whammy
ID: 8163611
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
Industry Leaders: 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!

 

Author Comment

by:baetha
ID: 8168257
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
 
LVL 2

Accepted Solution

by:
mikkolsoft earned 800 total points
ID: 8168568
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
 

Author Comment

by:baetha
ID: 8168919
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
 
LVL 2

Expert Comment

by:mikkolsoft
ID: 8169015
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question