I'm developing an e-commerce website for a client. There is a task that requires me to verify if user inputs an US address (something like "2933 Glen Crow Court, San Jose, CA 95148") or not. This must be done in javascript code. I learned regular expression before but haven't used it for a long time. I wonder if anyone can give me a regular expression that suits what I need.
Asked
Active
Viewed 3,514 times
1
-
3__"Give"__ you, no. __Help__ you, yes. What have you tried? – sshashank124 Apr 03 '14 at 08:28
-
yes i have tried a lot regex examples from google search but none of them suits. – php.dev.2014 Apr 03 '14 at 08:29
-
4"something like "2933 Glen Crow Court, San Jose, CA 95148""? Something like? Is it like that or is it not? If you want a regular expression the first thing you need to determine is the *exact* pattern you need to match. – Anthony Grist Apr 03 '14 at 08:29
-
well i need something like
(American style), , (only 2 characters) -
like example. 2933 Glen Crow Court, San Jose, CA 95148 – php.dev.2014 Apr 03 '14 at 08:31
-
3**1**. What's 'American Style'? **2**. When giving more info asked in the comments, please edit your question so that's it's clearer for everyone. **3**. Copy paste google examples is cool and all, but you should really try to write the regex yourself and come back when (if) it's not working. Your question will be clearer and you'll have more answers/enthusiastic welcome. – Robin Apr 03 '14 at 08:36
-
never mind. thanks anyway. I found a better way to validate the address without using annoying regular expression. – php.dev.2014 Apr 03 '14 at 08:50
-
it turns out that I can check by verifying the data that Google map api returns. anyway, thank you all for your reply. – php.dev.2014 Apr 03 '14 at 08:51
1 Answers
4
As you are in distress, help will be provided however it is advised that you show the attempt you have made before asking for information to be handed to you. From what I could get from your question, something along the follow regex would work:
/(\d+) ((\w+[ ,])+ ){2}([A-Z]){2} (\d){5}/
Breakdown
1) (\d+) - Match any number of digits at the start
2) ((\w+[ ,])+ ){2} - Match any alphanumeric characters followed by a space or a comma, exactly twice (once for street, once for city)
3) ([A-Z]){2} - Match 2 capital letters
4) (\d){5} - Match five digits
Demo

sshashank124
- 31,495
- 9
- 67
- 76
-
@php.dev.2014, If my answer was helpful, would you mind accepting it? Thanks – sshashank124 Apr 03 '14 at 09:35
-
Can u show me how to accept it pls? I saw the vote button but when I pressed it, it says I must have 15 reputation points. – php.dev.2014 Apr 03 '14 at 10:26