Solved

regex for street address format

Posted on 2009-05-18
5
1,354 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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 get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now