Hi all,
What is the best way to check if an email address is in the correct format? either using a regex or any other method?
Thanks in advance to any replies.
If you know you're looking at an email address and not something else, then a wildcard match using *@*.* would work, but I'm sure there's a better regex method. And if you don't know if it's an email address, then that would match things like an $address() (nick!ident@host.com), which you wouldn't want.
While regex probably is the best method, there is no best expression...
Should give you an idea:
http://www.regular-expressions.info/email.htmlhttp://regexlib.com/Search.aspx?k=&c=1&m=-1&ps=50
I am unfamiliar with regex, so had alot of trouble following that article. I don't need it to be amazing or even foolproof. I just want a simple check on a string to see if it follows the basic format of an email address.
I took the second expression from the first link, added separators and "case insensitive" flag, and set it into a custom identifier:
alias ismail {
var %r = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i
if ($regex($1,%r)) { return $true }
return $false
}
Now try
//echo -a $ismail(test) $ismail(@nothing) $ismail(123@456) $ismail(nick!ident@host.domain) $ismail(Test@something.com)
And use $ismail(<address>), like in
alias mailtest {
if ($ismail($input(Enter eMail address:,eog,Mail Address))) { echo -a valid address given: $! }
else { echo -a no/invalid address given }
}