0

I am trying to make a dynamic address builder to be used in combination with GMap Geocoder.

My code is like the following:

HTML

<input type="text" class="address" />
<input type="text" class="city" />
<input type="text" class="state" />
<input type="text" class="zipCode" />
<input type="text" class="country" />

JavaScript

$('.address, .city, .state, .zipCode, .country').blur(
    function()
    {
        var address = '';

        address += $('.address').val() + ', ';
        address += $('.city').val() + ', ';
        address += $('.state').val() + ', ';
        address += $('.zipCode').val() + ', ';
        address += $('.country').val();

        console.log(address);
    }
);

The problem now :

When I blur the address field I am getting the following in my console:

MyAddress, , , ,

also, in some case (at least for my area) there are two names for the same location and we use coma to separate them. In example the address can become something like that:

MyAddress, MySecondAddress, Cityname, State, zipCode, Country

The Question:

while the address is builded automatically, how can I remove the remaining commas from the address string by using regex?

2
  • 1
    Why don't you choose approach and only add the non-empty strings to address? Commented Sep 26, 2013 at 7:11
  • 1
    Is there any reason why you use the "class" parameter to identify your fields, instead of the "id" parameter? Commented Sep 26, 2013 at 7:22

3 Answers 3

3

One possible approach:

address = address.replace(/,(?:\s*,)+/g, ',').replace(/^,|,$/g, '');

But actually, I would probably do it a bit differently: collect all the non-empty values into an array, then join this array with ',':

var $addressFields = $('.address, .city, .state, .zipCode, .country');

$addressFields.blur(function() {
  var nonEmptyParts = $.map($addressFields, function(inp) {
    var inputStr = $.trim(inp.value);
    return inputStr === '' ? null : inputStr;
  });
  var fullAddress = nonEmptyParts.join(',');
  console.log(fullAddress);
});

I've employed a convenient feature of $.map here: when the callback function returns null (or undefined), the value is not added to the resulting array.

Sign up to request clarification or add additional context in comments.

1 Comment

The first approach didn't worked well. For some reason the second replace method didn't worked. I have try many other combinations for that, but still didn't work. Anyway, the second approach is what I need !! Thanks a lot :)
2

You should correct the root of the problem instead of removing the extra commas. In the first place you should not add the commas in this way; as you can see it's adding commas even if the field is empty.

Please try something like this:

HTML:

<form id="myform">
    <input type="text" class="address" name="address" />
    <input type="text" class="city" name="city" />
    <input type="text" class="state" name="state" />
    <input type="text" class="zipCode" name="zipCode" />
    <input type="text" class="country" name="country" />
</form>

JS:

var address = [];

// no need for that long line of selectors
$("#myform > input").blur(function(e){
    if ($(this).val()) { // only add this field if it has a value
        address.push($(this).val());
    }
    console.log(address.join(', ')); // tada, no more extra commas
});

If your form has other inputs, use a div to group the inputs instead.

Comments

0
address = address.replace(/( ?,)+/, '');

1 Comment

Using replace with a string param instead of regex one -> just a single replacement. Besides, even if this solution worked, it wouldn't remove the trailing commas.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.