Reputation: 668
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
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
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
Reputation: 37365
Use |
to recount all acceptable combinations:
/^([\w](\.|,|\.,)([\s]?)){1,60}$/
Upvotes: 1