Using the Address Field module, I have created a country/state/city formatter to allow users to input their location.

On my site, about half of the users are Japanese.

Address Field provides a select list of countries by default, and I have added select lists for states/provinces.

However, users input cities in a standard text field. I am using Location Taxonomize to tag users with their cities, so I want all cities to be in the same language (in this case, English). I already have some help text on the form that asks users to input their data in English, but I'd like to add form validation to make sure.

Many Japanese sites do this by checking whether the user input is single-byte or double-byte; since Japanese characters are single-byte and standard English characters are single-byte, this is sufficient for my purposes.

Is there a function for checking this in the form API?

3 Answers 3


You could compare the length of the string by using strlen which doesn't account for double-byte characters and then using drupal_strlen which does account for double-byte characters. If those functions think the length is different then there are some double-byte characters in there.

That comparison would need to go in a custom form_validate function.


I don't think there's anything like that in Drupal core, but assuming your PHP library has the Multibyte String (mbstring) library built in, you could check that mb_detect_encoding() returns "ASCII".

print mb_detect_encoding("Tokyo");
// Prints "ASCII"
print mb_detect_encoding("東京");
// Prints "UTF-8" (at least on my machine)

Another option would be to use something like preg_match(). The following example will set $has_invalid_chars to TRUE if $city_name contains any characters other than the English alphabet characters, the digits zero through nine, or the space character.

$has_invalid_chars = preg_match('/[^abcdefghijklmnopqrstuvwxyz0-9 ]/i', $city_name);

For reference, based on greggles' answer (accepted on this page) and Clive's answer here, here's the code that did it:

function MYCUSTOMMODULE_location_doublebyte_validation_handler($form, $form_state) {
  // Get the length 
 $length1 = strlen($form_state['values']['field_MYADDRESSFIELD']['und']['0']['locality']);
  $length2 = drupal_strlen($form_state['values']['field_gen_MYADDRESSFIELD']['und']['0']['locality']);

  if ($length1 !== $length2) {
    form_set_error('addressfield', t('City names can only contain English characters; please write Asian city names using the English alphabet (no Japanese or Chinese characters).'));

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.