I have the following regex for email validation: \b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$\b
Currently, it is saying this email format is valid: john@company....com
. How do I fix my regex so that it is invalid to have more than one "." character?
Asked
Active
Viewed 26 times
0

Sam Niles
- 31
- 5
-
2Remove the `.` from `[A-Z0-9.-]+`? Alternatively, you can use `[A-Z0-9.-]*[A-Z0-9-]+` if you want to support dots in the hostname (or `[A-Z0-9.-]+(?<!\.)` if Lookbehinds are supported). – 41686d6564 stands w. Palestine Sep 26 '22 at 23:53
-
When you change that you'll need to allow more than one repetition of `[A-Z0-9-]+\.`. Otherwise it won't match `company.co.uk`. – Barmar Sep 26 '22 at 23:54
-
Use a negative lookahead. Replace `\.` with `\.(?!\.)`. https://regex101.com/r/IzChAl/1 – code Sep 26 '22 at 23:54
-
@Barmar the OP's question is about consecutive periods. This may give what the OP needs but does not answer the question. – code Sep 26 '22 at 23:57
-
@code That won't work in this case because `\.(?!\.)` will match the _last_ dot anyway. – 41686d6564 stands w. Palestine Sep 26 '22 at 23:57
-
@code There's little point making incremental fixes like this when he could just do it the right way. – Barmar Sep 27 '22 at 00:00
-
@41686d6564 stands w. Palestine, thanks for the suggestion. I modified the regex to be: " \b[A-Z0-9._%+-]+@[A-Z0-9.-]*[A-Z0-9-]+\.[A-Z]{2,}$\b ". However this domain is still consider to be valid: joe@company.....co.uk – Sam Niles Sep 27 '22 at 20:17