Validate Email Address
Problem statement
Given a string email, return true when it satisfies all of these rules and false otherwise:
- It contains exactly one
@. - The local part before
@is nonempty and contains only lowercase letters, digits, dots, underscores, and hyphens. - The domain after
@contains at least two nonempty labels separated by dots. - Each domain label contains only lowercase letters, digits, and hyphens and cannot begin or end with a hyphen.
This exercise defines a bounded validation grammar; it is not full RFC email validation.
Function
isValidEmail(email: String) → booleanExamples
Example 1
email = "dev.team@example.com"return = trueThe address has one @, a valid local part, and two valid domain labels.
Example 2
email = "userexample.com"return = falseThe address does not contain an @.
Example 3
email = "user@-example.com"return = falseThe first domain label begins with a hyphen.
Constraints
1 <= email.length <= 10^5.emailcontains ASCII characters.