mIRC Homepage
Posted By: HaleyJ email validator - regex or other method - 09/08/10 10:51 AM
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.
Posted By: Riamus2 Re: email validator - regex or other method - 09/08/10 11:49 AM
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.
Posted By: Horstl Re: email validator - regex or other method - 09/08/10 01:15 PM
While regex probably is the best method, there is no best expression... smile
Should give you an idea:
http://www.regular-expressions.info/email.html
http://regexlib.com/Search.aspx?k=&c=1&m=-1&ps=50
Posted By: HaleyJ Re: email validator - regex or other method - 09/08/10 02:15 PM
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.
Posted By: Horstl Re: email validator - regex or other method - 09/08/10 02:29 PM
I took the second expression from the first link, added separators and "case insensitive" flag, and set it into a custom identifier:
Code:
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
Code:
//echo -a $ismail(test) $ismail(@nothing) $ismail(123@456) $ismail(nick!ident@host.domain) $ismail(Test@something.com)


And use $ismail(<address>), like in
Code:
alias mailtest {
  if ($ismail($input(Enter eMail address:,eog,Mail Address))) { echo -a valid address given: $! }
  else { echo -a no/invalid address given }
}

Posted By: HaleyJ Re: email validator - regex or other method - 09/08/10 03:13 PM
Thank You. Very helpful
© mIRC Discussion Forums