Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check Constraint enforcing a valid email address

Status
Not open for further replies.

Luzian

Programmer
Nov 27, 2005
103
US
I found a nice article that takes advantage of SQL Server 2005's CLR integration @
The code is pretty simple, although I've never gotten around to understanding regular expressions.

Code:
public partial class UserDefinedFunctions
{
    static readonly string emailRegex =
        @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]"
        + @"{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))"
        + @"([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";

    [Microsoft.SqlServer.Server.SqlFunction]
    public static SqlBoolean
       ValidateEmailAddress(SqlString emailAddress)
    {
        Regex regex = new Regex(emailRegex);
        return regex.IsMatch(emailAddress.Value);
    }
}
After deploying your SQL Server project (in visual studio 2005), slap your constraint on an email address column of some table

Code:
ALTER TABLE [Users]
ADD CHECK ([dbo].[ValidateEmailAddress]([email]) = (1))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top