Take the 2-minute tour ×

We need to implement the validation for the following field

Street Address/ Business Address

Conditions which needs to be taken care are as below

Post office box, private bag NOT acceptable as address. The address field should not start with the following:

NOTE: ^ = space

G.P.O. Box

P / O Box    
P/O Box    
P O Box    
P.O. Box

Private Bag^    
Private Bag.    
Locked Bag

This list needs to be configurable to allow for additional rules to be included at a later date. There is no need to validate for upper/lower case

Can you suggest me this kind of validation can be better implement using Javascript or Java?

  1. if I use Java, is it advisable to use Regex class

  2. If Javascript, what should be my way

Kindly share your sample code if you have any

I am checking if we use of Regex would be helpful in doing this?

ps: Since this project cant use any google or yahoo api or any other paid APIs to parse the street address.

share|improve this question
why did you tag this question with a 'php' tag? Does php bear any significance here? –  Ryan Fernandes May 30 '11 at 5:34
I doubt there's one regex that covers all those scenarios. Best if you use multiple matches for one or few conditions and do nested if-statements instead. –  Alex R. May 30 '11 at 5:34
I am using PHP as well in the development –  gmhk May 30 '11 at 5:53
Do you want to do this on the server or client side? I would Javascript on the client side, and php on the server side (if that is what you are using on the server) –  Kaj May 30 '11 at 6:31
add comment

2 Answers

up vote 2 down vote accepted

I wouldn't use regexp in that case. Instead I'd put all "stop words" in a file, read it into a set and use a loop to verify:

public static boolean isValid(String address) {
  Set<String> stopWords = getSet();  // some magic to get the loaded set
  for (String stopWord:stopWords) {
    if (address.trim().toLowerCase().startsWith(stopWord.toLowerCase())) {
       return false;
  return true;

Big advantage: the stop words are maintained in a file and not compiled into some regular expression that no one will understand next week and later.

share|improve this answer
Your solution worked great to me!!!! Thanks –  gmhk May 30 '11 at 8:14
add comment

Not a regex guru my self, so i my answer will be the long way of doing it.

you can include the list of possible values in a regex:

/(G\sP\sO)|(GPO\s)|(G\.P\.O)|(G\.P\.O\.\sBox)| .... and so one

I don't think it's the most efficient way of solving the problem but it will still work.

share|improve this answer
add comment

Your Answer


