Solved

regex for street address format

Posted on 2009-05-18
5
1,357 Views
Last Modified: 2012-05-07
I need the regex expression for verifying a street address.
Our requirements are:
- start with a number
- contain at least one letter
0
Comment
Question by:nap0leon
  • 4
5 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 24411430
A regular expression to match that pattern would be:


var re = new RegExp( '^[0-9]+ [A-Za-z]' )

Open in new window

0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24411443
The carot (i.e., '^' ) matches the "start of string",
the next thing (i.e., '[0-9]+') says one or more digits,
followed by a blank, and the last part (i.e., '[A-Za-z]') matches an upper or lowercase letter
0
 
LVL 41

Accepted Solution

by:
HonorGod earned 500 total points
ID: 24411468
e.g.,
<html>
<body>
 
Address: <input type='text' onchange='check(this)'>
 
<script type="text/javascript">
function check( obj ) {
  var re = new RegExp( '^[0-9]+ [A-Za-z]' )
  if ( re.test( obj.value ) ) {
    alert( 'ok' )
  } else {
    alert( 'invalid address' )
  }
}
</script>
 
</body>
</html>

Open in new window

0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24411824
Thanks for the grade & points.

Good luck & have a great day.
0
 

Expert Comment

by:123print
ID: 26944606
Um, no. That is not right. There are far more criteria involved in an address other than alpha numeric characters.

Look at:
/^([a-zA-Z0-9\.\-\ \/\#\\'])+$/

That should cover most of them. I am looking for a better one to cover PO Box disallows. But you have to consider special characters in addresses. Trust me on this one, I got pwnd on the phones after initial release of my code using a similar method to the accepted solution.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to count occurrences of each item in an array.

776 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