How to validate an email address
7 views (last 30 days)
Show older comments
Please help me write a programme to determine if a string is a valid email address.
defining a valid email address as name@domain.tld where "name" can contain the characters a-z, A-Z, 0-9, _, and . (dot). And "domain" can contain the characters a-z, and -. "name" and "domain" must be non-empty. "tld" can be "com", "org", or "net".
Aim is to return true if valid, otherwise false
0 Comments
Answers (2)
pratik gautam
on 23 Jul 2020
user_entry = "username@domain.com";
control = regexp(user_entry,'[a-z_]+@[a-z]+\.(com|net)')
if(numel(control)==1)
aa="worked"
else
aa="failed"
end
here you go, hope it might help someone else
0 Comments
rwanda mc
on 11 Jan 2022
The fully RFC 822 compliant regex is inefficient and obscure for validate email address because of its length. Fortunately, RFC 822 was superseded twice and the current specification for email addresses is RFC 5322. RFC 5322 leads to a regex that can be understood if studied for a few minutes and is efficient enough for actual use.
If you use HTML5, use this code:
<input type="email" name="email" required placeholder="Enter a valid email address">
0 Comments
See Also
Categories
Find more on Argument Definitions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!