4

I am trying to modify this script to accept , - and ' for this regex to validate addresses on a form is it written correctly?

^[a-zA-Z0-9\s\,\''\-]*$
casablanca
  • 69,683
  • 7
  • 133
  • 150
Amen
  • 713
  • 4
  • 15
  • 28
  • "is it written correctly?" - did you try it yourself? – casablanca Sep 21 '10 at 19:41
  • 4
    If you're talking about street addresses, validating them is not trivial. Addresses can appear in many, many formats, and even if something looks like an address it doesn't mean that it's an actual valid address (ie. "123 Main Street" looks like an address, but maybe #123 was skipped or the street only has addresses up to #90). – Daniel Vandersluis Sep 21 '10 at 19:43
  • In any event, your expression is "correct" in the sense that it is valid syntax and won't crash. However it is very unspecific and will match any string containing only letters, numbers, whitespace, commas, single quotes, or dashes. – Daniel Vandersluis Sep 21 '10 at 19:45
  • For example, there are streets in some cities called "38 1/2 Street" – Pointy Sep 21 '10 at 19:45
  • Possible duplicate of [PHP regexp US address](http://stackoverflow.com/questions/1381085/php-regexp-us-address). – Daniel Vandersluis Sep 21 '10 at 19:49

4 Answers4

18

It works but there are some redundant escapes.

You need not escape comma,single quote and hyphen. You escape only when the char has a special meaning and you want to use it literally. Inside a char class:

  • -is a meta char, but not when it appears at the start or at the end. In your case it appears at the end so it has lost its special meaning (of range making).
  • ] is a meta char which marks the end of char class. So if you want to make ] part of char class you need to escape it.

So you can write your regex as:

^[a-zA-Z0-9\s,'-]*$
codaddict
  • 445,704
  • 82
  • 492
  • 529
5

In the comments, Daniel Vandersluis has a really good point: Addresses can't be verified by merely a regex. The USPS has an entire division called CASS (Coding Accuracy Support System) dedicated to verifying address data. I work for SmartyStreets where we provide CASS-Certified services for people. In fact, if you're truly interested in good addresses, I'll help you personally in getting started (it's really easy with Javascript, for example. Just takes a minute or so.)

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Matt
  • 22,721
  • 17
  • 71
  • 112
4

Above answered Regex gives false when your input is "House No.-780". The user can add "." to the address, therefore we have to make regex which accepts dot also. You can use this regex

/^[a-zA-Z0-9\s,.'-]{3,}$/ . 

This regex accepts minimum three character and there is no limit on max characters. Characters may include a-z, A-Z alphabets, whitespace, comma(,), dot(.), apostrophe ('), and dash(-) symbols.

Pulkit Aggarwal
  • 2,554
  • 4
  • 23
  • 33
0

If you just want to accept any of those characters (or an empty string), then you can modify what you have currently to:

/^[a-z0-9\s,'-]*$/i

This is by no means a street address validator, however. I suggest you learn more about regular expressions from somewhere. http://www.regular-expressions.info/examples.html is a good place to start.

Robusto
  • 31,447
  • 8
  • 56
  • 77