To check if a string is an email in JavaScript, you can use regular expressions (regex). Here's an example code:
index.tsx221 chars9 lines
The isValidEmail()
function takes an email string as an argument and returns true
if the string matches the email format, otherwise false
. The regular expression /^[^\s@]+@[^\s@]+\.[^\s@]+$/
checks if the string starts with one or more characters that are not whitespace or @
, followed by @
, followed by one or more characters that are not whitespace or @
, followed by .
, and ends with one or more characters that are not whitespace or @
.
gistlibby LogSnag