views:

26

answers:

2

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\,\''\-]*$
A: 

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
+1  A: 

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
Thank you that worked.
Amen