diff --git a/colorbox.module b/colorbox.module
index 3ddfb48..149f061 100644
--- a/colorbox.module
+++ b/colorbox.module
@@ -310,15 +310,12 @@ function colorbox_field_formatter_settings_form($field, $instance, $view_mode, $
   );
   $element['colorbox_gallery_custom'] = array(
     '#title' => t('Custom gallery'),
-    '#type' => 'machine_name',
+    '#type' => 'textfield',
     '#maxlength' => 32,
     '#default_value' => $settings['colorbox_gallery_custom'],
-    '#description' => t('All images on a page with the same gallery value (rel attribute) will be grouped together. It must only contain lowercase letters, numbers, and underscores.'),
+    '#description' => t('All images on a page with the same gallery value (rel attribute) will be grouped together. It must only contain lowercase letters, numbers, hyphen and underscores.'),
+    '#element_validate' => array('colorbox_gallery_custom_validate'),
     '#required' => FALSE,
-    '#machine_name' => array(
-      'exists' => 'colorbox_gallery_exists',
-      'error' => t('The custom gallery field must only contain lowercase letters, numbers, and underscores.'),
-    ),
     '#states' => array(
       'visible' => array(
         ':input[name$="[settings_edit_form][settings][colorbox_gallery]"]' => array('value' => 'custom'),
@@ -385,6 +382,15 @@ function colorbox_field_formatter_settings_form($field, $instance, $view_mode, $
 }
 
 /**
+ * Validate function for colorbox_gallery_custom.
+ */
+function colorbox_gallery_custom_validate($element, &$form_state) {
+  if ($element['#value'] != drupal_html_class($element['#value'])) {
+    form_error($element, t('%name must only contain lowercase letters, numbers, hyphen and underscores.', array('%name' => $element['#title'])));
+  }
+}
+
+/**
  * Implements hook_field_formatter_settings_summary().
  */
 function colorbox_field_formatter_settings_summary($field, $instance, $view_mode) {
diff --git a/views/colorbox_handler_field_colorbox.inc b/views/colorbox_handler_field_colorbox.inc
index 9a5c112..27a2045 100644
--- a/views/colorbox_handler_field_colorbox.inc
+++ b/views/colorbox_handler_field_colorbox.inc
@@ -162,14 +162,14 @@ If you would like to have the characters %5B and %5D please use the html entity
     $tokens = $this->get_render_tokens($this->options['alter']);
     $popup = filter_xss_admin($this->options['popup']);
     $caption = filter_xss_admin($this->options['caption']);
-    $gallery = filter_xss_admin($this->options['custom_gid']);
+    $gallery = drupal_html_class($this->options['custom_gid']);
     $popup = strtr($popup, $tokens);
     $caption = strtr($caption, $tokens);
     $gallery = strtr($gallery, $tokens);
 
     $width = $this->options['width'] ? $this->options['width'] : '';
     $height = $this->options['height'] ? $this->options['height'] : '';
-    $gallery_id = !empty($this->options['custom_gid']) ? $gallery : ($this->options['gid'] ? 'gallery-' . $this->view->name : '');
+    $gallery_id = !empty($gallery) ? $gallery : ($this->options['gid'] ? 'gallery-' . $this->view->name : '');
     $link_text = $tokens["[{$this->options['trigger_field']}]"];
     $link_options = array(
       'html' => TRUE,
