diff --git includes/form.inc includes/form.inc
index 1388351..0fbd55e 100644
--- includes/form.inc
+++ includes/form.inc
@@ -1350,7 +1350,7 @@ function form_type_textfield_value($form, $edit = FALSE) {
   if ($edit !== FALSE) {
     // Equate $edit to the form value to ensure it's marked for
     // validation.
-    return str_replace(array("\r", "\n"), '', $edit);
+    return trim(str_replace(array("\r", "\n"), '', $edit));
   }
 }
 
diff --git modules/simpletest/tests/form.test modules/simpletest/tests/form.test
index a0973d7..389da15 100644
--- modules/simpletest/tests/form.test
+++ modules/simpletest/tests/form.test
@@ -346,6 +346,48 @@ class FormsFormCleanIdFunctionalTest extends DrupalWebTestCase {
 }
 
 /**
+ * Tests for form elements and their behaviors.
+ */
+class FormElementTest extends DrupalWebTestCase {
+
+  public static function getInfo() {
+    return array(
+      'name' => t('Form elements test'),
+      'description' => t('Tests for form elements and their behaviors'),
+      'group' => t('Form API'),
+    );
+  }
+
+  function setUp() {
+    parent::setUp('form_test');
+  }
+
+  /**
+   * Test that textfield values are trimmed.
+   */
+  function testTextfieldTrim() {
+    $value = $this->randomName();
+    // Create a form that can be processed and contains a textfield.
+    $form_id = $this->randomName();
+    $form_state = form_state_defaults();
+    $form['textfield'] = array(
+      '#type' => 'textfield',
+    );
+    // Set $value with opening and trailing whitespace as input for the textfield.
+    $form_state['input'] = array(
+      'textfield' => ' ' . $value . ' ',
+      'form_id' => $form_id,
+    );
+    // Prepare and process the form.
+    drupal_prepare_form($form_id, $form, $form_state);
+    drupal_process_form($form_id, $form, $form_state);
+    // Assure that the resulting form value does not contain the whitespace.
+    $this->assertEqual($value, $form_state['values']['textfield'], t('Textfield values are trimmed.'));
+  }
+
+}
+
+/**
  * Test using drupal_form_submit in a batch.
  */
 class FormAPITestCase extends DrupalWebTestCase {
