Always Use Short Name for State / Province / Region

This was developed for a customer using Address Autocomplete in Italy. The province was returned as “Città Metropolitana di Torino” where they wanted to use the province code instead (e.g. “TO”).

Note: This snippet is only intended for use with Gravity Forms’ default International Address field type.

This JavaScript snippet applies the short name rule PER FORM. Add it only to forms where you want this behavior. For a global PHP version, see: https://gravitywiz.com/snippet-library/gpaa-use-short-name-for-province-2/

Instructions

Code

Filename: gpaa-use-short-name-for-province.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 * Gravity Perks // Address Autocomplete // Always Use Short Name for State / Province / Region
 * https://gravitywiz.com/documentation/gravity-forms-address-autocompate/
 *
 * Instruction Video: https://www.loom.com/share/f39708854d504d32902b5fca29e73213
 *
 * This was developed for a customer using Address Autocomplete in Italy. The province was returned as
 * "Città Metropolitana di Torino" where they wanted to use the province code instead (e.g. "TO").
 *
 * Note: This snippet is only intended for use with Gravity Forms’ default International Address field type.
 *
 * This JavaScript snippet applies the short name rule PER FORM. Add it only to forms where you want this behavior.
 * For a global PHP version, see: https://gravitywiz.com/snippet-library/gpaa-use-short-name-for-province-2/
 */
gform.addFilter("gpaa_values", function (values, place) {
  for (var i = 0; i < place.address_components.length; i++) {
    var component = place.address_components[i];
    if (component.long_name === values.stateProvince) {
      values.stateProvince = component.short_name;
    }
  }
  return values;
});

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.