PHP regexp US address

前端 未结 6 1910
忘掉有多难
忘掉有多难 2021-01-12 19:07

I\'m trying to see what would be a good way to validate a US address, I know that there might be not a proper way of doing this, but I\'m going for the basic way: #

6条回答
  •  心在旅途
    2021-01-12 19:31

    Ask the user to enter parts of the address in separate fields (Street name, City, State, and Zip Code) and use whatever validation appropriate for such a field. This is the general practice.

    Alternatively, if you want simplest of regex that matches for four strings separated by three commas, try this:

    /^(.+),([^,]+),([^,]+),([^,]+)$/
    

    If things match, you can use additional pattern matching to check components of the address. There is no possible way to check the street address validity but you might be able to text postal codes and state codes.

提交回复
热议问题