Reddy
Reddy

Reputation: 3

javascript street address regex validation

Please help me to create the regular expression for the street address.
conditions are like
1. Only numbers,(/-) and [ABCDFGHJKLMNPRSTV] should allow.
2. Maximum two consecutive characters(letters) are allowed only after slash.
3. Should start with number only(max 4 numbers), rest is an optional if they enter: should follow the above mentioned criteria.
4. Possible combinations are like
12, 1567, 19/A, 123/B,12-45, 124-138, 12-14/A, 14/A-16, 12/A-16/B, 165/2, 123/1,
12-14/2, 14/2-16, 12/2-16/B, 11/A-12/1, 12/1-14/2, 0126/2-3, 125/BA, 345/BA/128,
248/12/A, 123/234/BA, 246/12/A/2

I tried with
"^\d{1,4}([/-]?\d{0,3}?[ABCDFGHJKLMNPRSTV]{0,2}?[/-]?\d{0,3}?
[ABCDFGHJKLMNPRSTVV]{0,2}?[/-]?\d{0,3}?[ABCDFGHJKLMNPRSTV]{0,2}?)$"

this code working for the above mentioned combinations but still allowing other combinations also,
character should not followed by "-(-C is wrong)" it should only followed by** '/'**
and i don't want enter more than 4 digits if its a single string**(Ex:: 1234 is right, now it is allowing 12345....to 13 chars)**
and only two characters allowed if its preceded with '/'(Now 12ABCDABCD.. allowing which is not right).

Please help me to create a proper regular expression, thanks in advance.

Upvotes: 0

Views: 5055

Answers (2)

k0hamed
k0hamed

Reputation: 502

try this: ^[0-9]{1,4}(([\-\/][0-9]{1,4})|(\/[ABCDFGHJKLMNPRSTV]{1,2}))*$

  • [0-9]{1,4} : should start with numbers of length 1 to 4
  • [\-\/][0-9]{1,4}) : accepts - or / followed by numbers (1 to 4 length)
  • (\/[ABCDFGHJKLMNPRSTV]{1,2}) : accepts / followed by 1 or 2 of the characters inside the []
  • (([\-\/][0-9]{1,4})|(\/[ABCDFGHJKLMNPRSTV]{1,2}))*: to accept the last 2 pattern 0 or more times (one of them each time)

Upvotes: 1

Sanu Khan
Sanu Khan

Reputation: 100

Hope this will help

\d{1,5}\s\w.\s(\b\w*\b\s){1,2}\w*\.

This regex will validate address like

999 S. ABCD St.
eg: 123 N. Snow St.

Thanks,

Upvotes: 0

Related Questions