Validate an Email Address

by Edward 27 February 2010 08:02

In most cases it is "good practice" to validate the user input because of many reasons (i.e. security, reliability, spamming, etc.) - email validation is probarly the most common validation of them all.

The following regular expression(regex) will check for a valid email address in which Only letters (a-z or A-Z), numbers (0-9), hyphens (-), underscore (_) and periods (.) are allowed and no special characters are accepted.

   1:              // Write to console
   2:              Console.Write("Check Email Address: ");
   3:              
   4:              // get input
   5:              string inputEmail = Console.ReadLine();
   6:              string sValid;
   7:              //regular expression - change this to your requirement
   8:              string sRegex = @"^([a-zA-Z0-9_-.]+)@(([[0-9]{1,3}" +
   9:                    @".[0-9]{1,3}.[0-9]{1,3}.)|(([a-zA-Z0-9-]+" +
  10:                    @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$";
  11:              Regex reg = new Regex(sRegex);
  12:              
  13:              if (reg.IsMatch(inputEmail))
  14:              {
  15:                  sValid = "valid";
  16:              }
  17:              else
  18:              {
  19:                  sValid = "not valid";
  20:              }
  21:              
  22:              Console.WriteLine("Email Address " + 
  23:                  inputEmail + " is " + sValid);
  24:              Console.Read();

Tags: , , ,

ASP.NET | Social Media

String.IsNullOrWhiteSpace - New method available in .NET Framework 4

by Edward 19 February 2010 14:11

String.IsNullOrWhiteSpace - New method available in .NET Framework 4

I found a very nice addition to the .NET Framework(version 4) while experimenting with code in Visual Studio 2010. There is a new method called "IsNullOrWhiteSpace", which is more powerful than the more familiar "IsNullOrEmpty" method. Incase you were wondering, the "IsNullOrEmpty" method is still available to be used.

For example, the common whitespace symbol " " represents a blank space, as used between words and sentences. The most common whitespace characters may be typed via the space bar or the Tab key. Depending on context, a line-break generated by the Return key (Enter key) may be considered whitespace as well. This brings concerns when using the "IsNullOrEmpty" method.

Introducing "IsNullOrWhiteSpace"! In the .NET Framework 4, string.IsNullOrWhiteSpace() will return true if a string is full of whitespace characters. I found this very helpful when dealing with getting XML data from external web services.

Here is a short example of how to use this new method.

   1:          static void Main(string[] args)
   2:          {
   3:              // set some test values
   4:              string[] testValues = { null, String.Empty, "ABCDE", 
   5:                            new String(' ', 20), "  	   ", 
   6:                            new String(' ', 10) };
   7:              //loop through the list and return result
   8:              foreach (string value in testValues)
   9:              {
  10:                  Console.WriteLine("Return is: " + 
  11:              String.IsNullOrWhiteSpace(value));
  12:              }
  13:              // we want to see the return
  14:              Console.ReadLine();
  15:          }

 

Tags: , , , ,

ASP.NET | Development Resources

Calculate the amount of days between two dates

by Edward 04 February 2010 17:47

DateTime.Subtract is an easy method that sometimes get overlooked for how 'powerful' it can be. If you would like to calculate the amount of days between two date fields - of type DateTime - an easy approach is to use the "subtract" method and 'TimeSpan' class. The TimeSpan class represents a time interval, and has a dual set of properties, one set represents the time durations in integers and another set of properties, with the names prefixed with "total" represents the result in fractional values.

This is a quick example to find the number of days between two date fields.

   1:  TimeSpan timespan = Convert.ToDateTime("2010-02-04")
2: .Subtract(Convert.ToDateTime("2010-01-31"));
   3:  Console.WriteLine(timespan.Days+1);

Before subtracting DateTime objects, ensure that the objects represent times in the same time zone. Otherwise, the result will include the difference between time zones.

Tags: , ,

ASP.NET | Other

Dascode.Net now has comments enabled

by Edward 03 February 2010 17:28

My blog now has comments enabled, thanks to the latest version of Blogengine.net 1.6 that has moderation of comments. All new posts will have this functionality enabled, to allow visitors to comment on my blog entries. I will also be updating my site over the next few weeks, to improve the layout and some backend functionality. I hope you will continue to visit my site.

The following new features are now available in Blogengine.net v1.6:

  • Centralized Comment Management
  • Automated Comment Spam Filtering with ability to plug-in custom Filtering modules
  • Multiple Widget Zones (details)
  • Referrers data and Blogroll items now stored in Database when using the DB blog provider.
  • Unsubscribe Link in Comment Notification Emails
  • Referrer Data can be Stored for more than 7 days.
  • Blogroll items can now be Ordered.
  • Newsletter Widget more Intelligent - Emails sent when a post is going from an Unpublished to Published state.
  • Twitter Widget - New options and improvements
  • Page Slugs now saved in Database.
  • New Logging system to Track events and errors.
  • Unhandled Exception Handling
  • Fixes to Comment Notification Emails not being sent out correctly in some cases.
  • Outgoing Email improvements
  • Many other improvements and fixes

You can download the latest version of Blogengine.Net here.

Tags: , , ,

Other

About DasCode.Net

I'm a ASP.NET web developer and code enthusiast. Blogging about everything .Net related.

Code... that's .net

Month List