Simple Email Validation

Medium~15 min

Implement a simplified email validator. Return true if the string is a valid email address according to these rules:

  1. Exactly one @ symbol — not zero, not two or more
  2. Non-empty local part — the part before @ must have at least one character
  3. Non-empty domain — the part after @ must have at least one character
  4. Domain has at least one dot — the domain part must contain at least one '.'
  5. No spaces anywhere — the entire string must be free of spaces
  6. Domain does not start or end with a dot"user@.com" and "user@com." are invalid
  7. No consecutive dots in domain"user@a..b" is invalid

This is a simplified check — it does not cover the full RFC 5322 email spec.

Examples

Example 1
Input: s = "user@example.com"
Output: true
Explanation: Has one @, non-empty local and domain parts, domain has a dot.
Example 2
Input: s = "invalid"
Output: false
Explanation: No @ symbol present.
Example 3
Input: s = "@example.com"
Output: false
Explanation: Local part (before @) is empty.

Constraints

  • 0 <= s.length <= 200
  • s consists of printable ASCII characters.
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run