Skip to content

Email validation has issues: no + or - no domains longer than 3 chars #13

@MischaKr

Description

@MischaKr

File:
Form.php

Current:
'email' => '/^\w+([.-]?\w+)@\w+([.-]?\w+)(.\w{2,3})+$/',

AI analysis:

Key points:

  • ^\w+ → Local part must start with one or more “word” characters (letters, digits, underscore only — no plus sign allowed here).
  • ([.-]?\w+)* → Allows optional dot or hyphen, followed by word characters, repeated any number of times.
  • Domain part (@\w+([.-]?\w+)*) → Same dot/hyphen rules as local part.
  • (.\w{2,3})+ → Top‑level domain must be 2 or 3 “word” chars only (e.g., .com, .nz) — cannot match .info or longer TLDs.

So loads of things that are not ideal with this regex.

Proposed:

'email' => '/^(?!.)(?!.*..)[A-Za-z0-9._%+-]+(?<!.)@(?:(?!-)[A-Za-z0-9-]{1,63}(?<!-).)+[A-Za-z]{2,}$/',

Simple test:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions