Creating module with autocomplete and accepting multiple values in textfield
You can use the following code.

function _autosearch($string) {
  $matches = array();

  if ($string) {
    $items = array_map('trim', explode(',', $string));
    $last_item = array_pop($items);
    $prefix = implode(', ', $items);

    $result = db_select('users')-&gt;fields('users', array('name'))-&gt;condition('name', '%' . db_like($last_item) . '%', 'LIKE')-&gt;range(0, 10)-&gt;execute();
    foreach ($result as $user) {
      $value = !empty($prefix) ? $prefix . ', ' . $user-&gt;name : $user-&gt;name;
      $matches[$value] = check_plain($value);
    }
  }

  drupal_json_output($matches);
}


This is the result.







If you want to avoid a username can be used more than once, then you can use the following code.

function _autosearch($string = '') {
  $matches = array();

  if ($string) {
    $items = array_map('trim', explode(',', $string));
    $last_item = array_pop($items);
    $prefix = implode(', ', $items);

    $result = db_select('users')-&gt;fields('users', array('name'))-&gt;condition('name', '%' . db_like($last_item) . '%', 'LIKE')-&gt;range(0, 10)-&gt;execute();
    foreach ($result as $user) {
      if (!in_array($user-&gt;name, $items)) {
        $value = !empty($prefix) ? $prefix . ', ' . $user-&gt;name : $user-&gt;name;
        $matches[$value] = check_plain($value);
      }
    }
  }

  drupal_json_output($matches);
}