This is an automated email from the ASF dual-hosted git repository.

reiern70 pushed a commit to branch email-pattern-1884-10.x
in repository https://gitbox.apache.org/repos/asf/wicket.git

commit 3496aee26f877359c0fe7e72a29dfdf18394e664
Author: reiern70 <[email protected]>
AuthorDate: Thu Nov 12 18:04:05 2026 -0500

    Escape the hyphens in the pattern of EmailAddressValidator
    
    The HTML5Attributes behavior renders the pattern of every PatternValidator
    of a text component into the HTML pattern attribute. Browsers compile that
    attribute as a JavaScript regular expression with the v flag, where a
    hyphen left unescaped in a character class is a syntax error. The pattern
    of EmailAddressValidator had four such classes ([_A-Za-z0-9-]), so on an
    EmailTextField with HTML5Attributes Chrome logged "Invalid regular
    expression" on every page and again on each submit, and Chrome and Firefox
    both ignored the attribute.
    
    The hyphens are escaped now. Java reads the pattern as before and it
    matches the same addresses; the browser compiles it and enforces it. An
    application that adds HTML5Attributes to an EmailTextField now sees the
    browser reject an address such as "name@host" before the form is
    submitted, with the browser's own message, where the request used to
    reach the server and fail EmailAddressValidator there. Without
    HTML5Attributes nothing changes. The javadoc of PatternValidator now says
    that its pattern can be rendered for the browser and has to be valid
    there as well.
    
    GitHub issue #1884
---
 .../apache/wicket/validation/validator/EmailValidatorTest.java    | 2 +-
 .../apache/wicket/validation/validator/EmailAddressValidator.java | 2 +-
 .../org/apache/wicket/validation/validator/PatternValidator.java  | 8 ++++++++
 3 files changed, 10 insertions(+), 2 deletions(-)

diff --git 
a/wicket-core-tests/src/test/java/org/apache/wicket/validation/validator/EmailValidatorTest.java
 
b/wicket-core-tests/src/test/java/org/apache/wicket/validation/validator/EmailValidatorTest.java
index 55e353a850..cda9170e68 100644
--- 
a/wicket-core-tests/src/test/java/org/apache/wicket/validation/validator/EmailValidatorTest.java
+++ 
b/wicket-core-tests/src/test/java/org/apache/wicket/validation/validator/EmailValidatorTest.java
@@ -36,7 +36,7 @@ class EmailValidatorTest
        {
                EmailAddressValidator test = new EmailAddressValidator();
                String[] emails = new String[] { "[email protected]", 
"[email protected]",
-                               "[email protected]", "[email protected]", "[email protected]" };
+                               "[email protected]", "[email protected]", "[email protected]", 
"[email protected]" };
                for (String email : emails)
                {
                        assertTrue(test.getPattern().matcher(email).matches(), 
email + " should be valid");
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/validation/validator/EmailAddressValidator.java
 
b/wicket-core/src/main/java/org/apache/wicket/validation/validator/EmailAddressValidator.java
index a9d5b03b3f..4415aab3dd 100644
--- 
a/wicket-core/src/main/java/org/apache/wicket/validation/validator/EmailAddressValidator.java
+++ 
b/wicket-core/src/main/java/org/apache/wicket/validation/validator/EmailAddressValidator.java
@@ -59,7 +59,7 @@ public class EmailAddressValidator extends PatternValidator
        protected EmailAddressValidator()
        {
                super(
-                       
"^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*((\\.[A-Za-z]{2,}){1}$)",
+                       
"^[_A-Za-z0-9\\-]+(\\.[_A-Za-z0-9\\-]+)*@[A-Za-z0-9\\-]+(\\.[A-Za-z0-9\\-]+)*((\\.[A-Za-z]{2,}){1}$)",
                        Pattern.CASE_INSENSITIVE);
        }
 }
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/validation/validator/PatternValidator.java
 
b/wicket-core/src/main/java/org/apache/wicket/validation/validator/PatternValidator.java
index aac78881ec..d0dca7a5e5 100644
--- 
a/wicket-core/src/main/java/org/apache/wicket/validation/validator/PatternValidator.java
+++ 
b/wicket-core/src/main/java/org/apache/wicket/validation/validator/PatternValidator.java
@@ -42,6 +42,14 @@ import org.apache.wicket.validation.ValidationError;
  * The error message will be generated with the key "PatternValidator" and one 
additional message
  * key ${pattern} for the pattern which failed to match. See {@link 
FormComponent} for a list of
  * further messages keys.
+ * <p>
+ * The {@link org.apache.wicket.markup.html.HTML5Attributes} behavior renders 
the pattern of a
+ * text component's <code>PatternValidator</code> into the HTML 
<code>pattern</code> attribute, so
+ * the browser checks the input too. The browser compiles that attribute as a 
JavaScript regular
+ * expression with the <code>v</code> flag and ignores it when it does not 
compile, so the pattern
+ * has to be valid there as well: escape a <code>-</code> in a character class
+ * (<code>[a-z\-]</code>), for example. Compile flags such as {@link 
Pattern#CASE_INSENSITIVE} are
+ * not rendered.
  * 
  * @see java.util.regex.Pattern
  * 

Reply via email to