Reputation: 1
I'd like to validate a street address using javascript. The address must be adhere to a Address 12
like format where it begins with one or more characters, followed by a space and then followed by at least one digit.
I'm using the following regexp:
(/[a-zA-Z+\s?\d+$]/.test(field)
and various modifications of these, but is not working well. How can i re-write to be correct?
Thanks a lot.
Upvotes: 0
Views: 641
Reputation:
You are using []
which denotes character class. Only one out of many characters defined in it will be matched.
In your case it's matching only A.
You will have to use /\w+ \d+/
which means Any number of characters
space
one or more digits
Upvotes: 0
Reputation: 2061
You seem to be mixing bracketed character sets with other regex commands, such as optional and repeating. Something like this would match a word followed by a space by a number:
/^[a-zA-Z]+\s[0-9]+$/.test(field_value)
That said, I don't know any address format that has a single word followed by a number, without allowing for, for example, multiple words in the street name. Which could be achieved like this:
/^([a-zA-Z]+\s)+[0-9]+$/
Upvotes: 1