none
Regular expression for email validation in CLR(C#.net) programming in SSIS RRS feed

  • Question

  • I have a requirement in CLR(C#) programming in SSIS to validate email address with below rules: I have the regex in PHP that I need to convert it into CLR(C#) code.

    1.Must have one and only one (1) @ symbol

    2.Must have only letters, numbers, dashes - or dots . after the @ symbol

    3.Must have 2 parts on either side of the dot . i.e. @a.b is valid or @hotmail.com or @gmail.com but a. or ..b are not valid

    4.Must have 2 parts on either side of a dash - i.e. a-b is valid but a- or -b is not valid

    5.It can be any character before @.

    The regular expression in PHP is:

    @0-9A-Za-z(.0-9A-Za-z)+

    Can someone please advise on how to do this?

    Tuesday, April 3, 2018 6:49 AM

Answers

  • Did this question get asked just a while back? Your rules do not properly validate email addresses. Are you trying to validate email addresses or trying to validate a string value that is email-like?

    For validating email addresses use MailAddress.

    try
    {
       var address = new MailAddress("abc@def.ghi");
    } catch (FormatException)
    {
       //Not valid
    };

    For validating email-like strings based upon the RE that you posted try this.

    @".+@\w+(\.\w+)+"
    

    Also, your RE doesn't line up with the rules you specified. You mentioned allowing dashes but there are no dashes in your RE. You also mentioned 


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, April 3, 2018 2:15 PM
    Moderator