Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1369
  • Last Modified:

regex for street address format

I need the regex expression for verifying a street address.
Our requirements are:
- start with a number
- contain at least one letter
0
nap0leon
Asked:
nap0leon
  • 4
1 Solution
 
HonorGodCommented:
A regular expression to match that pattern would be:


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

Open in new window

0
 
HonorGodCommented:
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
 
HonorGodCommented:
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
 
HonorGodCommented:
Thanks for the grade & points.

Good luck & have a great day.
0
 
123printCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

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