Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple regex for street address

Tags:

I know there are a dozen questions at least on this. I am trying to have a simple validation of input for street address using Regex whereby I check for at least two spaces in the input entry. The reason? for the most part us addresses are at least 3 parts, street number, street name, type (lane, drive, ave , st ,etc)

I want to alert the user if the entry doesn't match at least that, if it has more than three spaces, meaning it has more names in the address, that's fine but the minimum not being met necessitates an alert. My latest effort is below, and is not working.

  var addregex = new RegExp("^\d{1,6}\040([A-Z]{1}[a-z]{1,}\040[A-Z]{1}[a-z]{1,})$|^\d{1,6}\040([A-Z]{1}[a-z]{1,}\040[A-Z]{1}[a-z]{1,}\040[A-Z]{1}[a-z]{1,})$|^\d{1,6}\040([A-Z]{1}[a-z]{1,}\040[A-Z]{1}[a-z]{1,}\040[A-Z]{1}[a-z]{1,}\040[A-Z]{1}[a-z]{1,})$");

            if (addregex.test($(this).val())) {
                alert('is valid');
                address.addClass('isvalid');
                address.css("border", "1px solid lightgray");
            } else {
                address.css("border", "2px solid red");
                alert("Are you sure this is a valid street address?");
                address.focus();
            }