Design comparison
Solution retrospective
- Having little bit trouble with Regular Expressions in Javascript.
- Bugs in email field, getting submitted even its input is not valid. *e.g: abcd@gmail *
Community feedback
- @Daviddp96Posted about 1 year ago
Hey there,
Here's the fix to your regular expression:
const mailFormat = /^[a-zA-Z0-9._]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*\.[a-zA-Z]{2,}$/;
The difference is that now it includes
\.[a-zA-Z]{2,}
at the endThis ensures that the domain extension consists of at least two alphabetic characters. As a result, only email addresses with a valid domain extension like ".com", ".net", or ".org" will be considered valid.
Also since it's a submit button you could consider using the submit event instead of the click event.
Cheers
Marked as helpful0@souravs97Posted about 1 year ago@David35D Thanks for feedback. Can you also elaborate this expression for me, cuz i just copy pasted it ... (?:.[a-zA-Z0-9-]+)* . Reply appreciated.
0@Daviddp96Posted about 1 year ago@souravs97 Here's a breakdown:
(?:) - Non-capturing group: allows grouping of multiple elements without capturing them as separate groups.
. - matches any character except a newline character. It can match letters, digits, symbols, or whitespace.
[a-zA-Z0-9-]+ - Character class: matches any uppercase letter (A-Z), lowercase letter (a-z), digit (0-9), or hyphen (-). The + quantifier specifies that the preceding character class should occur one or more times.
-
- Zero or more occurrences: It allows for optional repetition.
Putting it all together, (?:.[a-zA-Z0-9-]+)* matches any character followed by a sequence of one or more alphanumeric characters or hyphens. This sequence can repeat zero or more times.
Marked as helpful0 -
- @NehalSahu8055Posted about 1 year ago
- Use this regex
const mailFormat = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
Marked as helpful0@souravs97Posted about 1 year ago@NehalSahu8055 Thanks for feedback. Can you also elaborate this expression for me, cuz i just copy pasted it ... (?:.[a-zA-Z0-9-]+)* . Reply appreciated :)
0
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord