Email Validator - Problem

You are given a string and need to validate if it represents a valid email format.

A valid email must satisfy all of the following conditions:

  • Contains exactly one '@' symbol
  • Has a domain part after the '@' (at least one character)
  • Contains no spaces anywhere in the string
  • Uses only valid characters: letters (a-z, A-Z), digits (0-9), dots (.), hyphens (-), underscores (_)
  • Does not start or end with a dot

Return true if the email is valid, false otherwise.

Input & Output

Example 1 — Valid Email
$ Input: email = "user@domain.com"
Output: true
💡 Note: Has exactly one '@' at position 4, domain 'domain.com' exists, no spaces, all characters are valid (letters, dot), doesn't start or end with dot
Example 2 — Invalid: Multiple @ Symbols
$ Input: email = "user@@domain.com"
Output: false
💡 Note: Contains two '@' symbols, which violates the rule of exactly one '@' symbol
Example 3 — Invalid: Contains Spaces
$ Input: email = "user @domain.com"
Output: false
💡 Note: Contains a space character before '@', which is not allowed in valid email format

Constraints

  • 1 ≤ email.length ≤ 1000
  • email consists of printable ASCII characters

Visualization

Tap to expand
INPUTVALIDATIONRESULTEmail String:user@domain.comLength: 15 charactersContains: letters, @, dot1Count @ symbolsFound exactly 1 @2Check domain existsdomain.com found3Validate charactersAll chars valid4Check spaces & boundsNo spaces, valid boundsTRUEValid EmailAll validationrules passedKey Insight:A single pass through the string can validate all email rules simultaneously,checking @ count, character validity, and positioning in O(n) time.TutorialsPoint - Email Validator | Single Pass Validation
Asked in
Microsoft 35 Google 28 Facebook 22 Amazon 18
23.4K Views
Medium Frequency
~15 min Avg. Time
845 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen