Jahm
Jahm

Reputation: 668

Regular expression for home address

Can anyone help me with regular expression for checking home address please?

I came up with /^([\w]([\.,]?)([\s]?)){1,60}$/. It does the job except for addresses with ., on it.

Eg: Cabalan Rd.,

Is there any way that it could include ., , and/or both .,?

Upvotes: 1

Views: 315

Answers (4)

Ibrahim Najjar
Ibrahim Najjar

Reputation: 19423

Well you can improve your expression a bit more:

/^(?:\w+\.?,?\s?){1,60}$/
  • \w+: Matches one or more word characters.
  • \.?,?: Tries to match a single ., , or ., or nothing at all.
  • \s?: An optional space.
  • (?: ... ){1,60}: Match the previous items between 1 and 60 times.

Upvotes: 1

Alex Filipovici
Alex Filipovici

Reputation: 32561

Try this:

^([\w]([\.,]*)([\s]?)){1,60}$

Regular expression visualization

Debuggex Demo

Upvotes: 2

anubhava
anubhava

Reputation: 785108

Try changing your regex to:

/^([\w.,\s]){1,60}$/

But have to say that validating address with regex is not such a great idea.

Upvotes: 1

Alma Do
Alma Do

Reputation: 37365

Use | to recount all acceptable combinations:

/^([\w](\.|,|\.,)([\s]?)){1,60}$/

Upvotes: 1

Related Questions