|
Technology -
Coding
|
|
Written by Romeo Dumitrescu
|
|
Friday, 16 January 2009 07:20 |
|
For some time now, I've been regularly visiting the AppDev site to checkout the new free and on-demand training courses from the AppDev experts. It is true that some of the videos there have titles that do not reflect the exact course aims (some of the "Advanced" topics there should be renamed to "Novice"), but I find that the quality is overall good. I've directed a lot of friends towards the AppDev site because it covers good ground in it's courses and for most of them it's easier to download a well explained video course than to learn from books. (don't get me wrong, books and Google are the most important tools in a developer's arsenal, but books can be difficult if you've just started learning a programming language or don't have experience in software development) If you've got the time, visit http://www.appdev.com/ I assure you that you won't be disappointed, regardless of your training level. I'd love to get some feedback on this, if you can spare a moment or two, just post a comment to this article and state if you've found AppDev useful or not. Cheers!
|
|
Technology -
Coding
|
|
Written by Romeo Dumitrescu
|
|
Thursday, 15 January 2009 10:58 |
|
A friend of mine asked me yesterday how can one validate an e-mail address in C#. He gave me the following snippet he was using: public static bool IsValidEmailAddress(string sEmail)
{
if (sEmail == null)
{
return false;
}
int nFirstAT = sEmail.IndexOf('@');
int nLastAT = sEmail.LastIndexOf('@');
if ((nFirstAT > 0) && (nLastAT == nFirstAT) && (nFirstAT < (sEmail.Length - 1)))
{
// address is ok regarding the single @ sign
return (Regex.IsMatch(sEmail, @"(\w+)@(\w+)\.(\w+)"));
}
else
{
return false;
}
}
While this works rather well, there is a simpler way:
public static bool IsValidEmailAddress(string sEmail)
{
try
{
MailAddress copy = new MailAddress(sEmail);
return true;
}
catch
{
return false;
}
}
In order to use this, you'll need to add:
If you use the Regex solution, you have to make sure you consider all scenarios because, as we all know, e-mail addresses vary according to the domain mask and the e-mail identifier.
The above solution is much simpler and more efficient.
Explanation: If the MailAddress constructor fails to create an MailAddress object due to an invalid string, it will throw an FormatException exception.
You can extend this method by adding encoding and other options.
More info on the MailAddress Class can be found here: http://msdn.microsoft.com/en-us/library/system.net.mail.mailaddress.aspx
|
|
|
|
|
<< Start < Prev 1 2 3 4 5 6 7 8 9 Next > End >>
|