diff --git a/core/lib/Drupal/Core/Render/Element/MachineName.php b/core/lib/Drupal/Core/Render/Element/MachineName.php
index 02b1fbff858be73c8e5e71cbe419486ee563ffd7..616d1961b787ffa5df56fffc3320baf4194da569 100644
--- a/core/lib/Drupal/Core/Render/Element/MachineName.php
+++ b/core/lib/Drupal/Core/Render/Element/MachineName.php
@@ -245,7 +245,7 @@ public static function processMachineName(&$element, FormStateInterface $form_st
    */
   public static function validateMachineName(&$element, FormStateInterface $form_state, &$complete_form) {
     // Verify that the machine name not only consists of replacement tokens.
-    if (preg_match('@^' . $element['#machine_name']['replace'] . '+$@', $element['#value'])) {
+    if (!empty($element['#machine_name']['replace']) && preg_match('@^' . $element['#machine_name']['replace'] . '+$@', $element['#value'])) {
       $form_state->setError($element, t('The machine-readable name must contain unique characters.'));
     }