Plunging into .NET Development

Weblog Pieter Gheysens
Microsoft .NET Development - C# - Enterprise Library - Visual Studio 2005 Team System - Compuware DevPartner - ...
 


Saturday, December 30

Check valid email address - client/server

Validation controls in .NET 2.0 provide an easy-to-use mechanism for all common types of standard validation. You can drag and drop for instance the RegularExpressionValidator control on your webform to determine whether the value of an input control matches a pattern defined by a regular expression. Ideal to check a telephone number or an email address. This validation occurs by default on client-side and on server-side.

Lately I needed to create a webpage where an input field was needed where multiple email addresses could be entered, separated with commas. In this particular case, I only provided validation on server-side using the Regex class (namespace System.Text.RegularExpressions).

    public void DoValidationForEmails(string inputString)

    {

      //inputString contains all email addresses, separated with commas

      string separator = ",";

      string[] allEmails = inputString.Split(separator.ToCharArray());

 

      //Regular Expression for validating email addresses

      Regex regularExpression = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");

 

      foreach (string emailAddress in allEmails)

      {

        if (regularExpression.IsMatch(emailAddress))

        {

          //Valid email address

        }

        else

        {

          //Invalid email address

        }

      }

    }


Anyone already done this on client-side as well?

Labels:

2 Comments:

  • At 8:18 PM, Anonymous Anonymous said…

    function DoValidationForEmails(inputString)
    {
    var separator = ',';
    var regularExpression = new RegExp('\w+([-+.\']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*');

    var allEmails = inputString.split(separator);
    for(var i = 0; i < allEmails.length; ++i)
    {
    var emailAddress = allEmails[i];
    if (regularExpression.test(emailAddress))
    {
    // valid e-mail address
    } else
    {
    // invalid e-mail adress
    }
    }
    }

     
  • At 8:24 PM, Blogger Pieter said…

    Great! Good job Tim!

     

Post a Comment

<< Home