-1

Newbie to C# and seem to have an issue. Im amending a program that has the below code validating email address':

public static bool IsValidEmail(string sEmail)
    {
        sEmail = sEmail.Trim();
        if (sEmail == string.Empty)
        {
            return false;
        }
return (Regex.IsMatch(sEmail, @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
                       + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
                       + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
                       + @"([a-zA-Z]+[\w-]*\.)+[a-zA-Z]{1,4})$"));

Now since this was implemented email validation has changed and now i want to only validate on there being an @ and a . in the address.

I tried:

   public static bool IsValidEmail(string sEmail)
     if ((sEmail.IndexOf("@") != -1) & (sEmail.IndexOf(".") != -1))
        {
            return true;
        }
        else
        {
            return false;
        }

but a should be now valid address ie name+@domain.com is still being recieved as bad.

Any help would be appreciated.

Gerry85
  • 71
  • 2
  • 9
  • 1
    this is not duplicate. its a syntax problem. – Royi Namir Apr 03 '13 at 12:17
  • @RoyiNamir - You should not be trying to use a regex to valid email addresses. – keyboardP Apr 03 '13 at 12:17
  • @keyboardP tell it to Microsoft asp.net validators of email patterns., they use it. and I personally use it also. and in fact its not the issue. he had a syntax prob that's all. – Royi Namir Apr 03 '13 at 12:18
  • 1
    @RoyiNamir - There's a difference between custom regexes and standard .NET ones. One is more likely to be less consistent. – keyboardP Apr 03 '13 at 12:19
  • 1
    @keyboardP We can argue about the pattern. yes. but saying _You should not be trying to use a regex to valid email addresses._ - well that's another thing... – Royi Namir Apr 03 '13 at 12:21
  • 1
    @RoyiNamir - That's fair, I should've clarified with "your own" regexes. A key reason is because if OP goes on to then create a `MailAddress` object that doesn't validate against the .NET regex but does against his, an exception will be thrown anyway. – keyboardP Apr 03 '13 at 12:22
  • 2
    This is obviously not your real code - it does not compile (missing braces to define the method). Furthermore, the code you have posted would validate `name+@domain.com` as a valid email address. Please post your *real* code and your *real* problem. – RB. Apr 03 '13 at 12:22
  • This is a snippet from a larger application where this segment was doing the validation. This was not originally written by myself. – Gerry85 Apr 03 '13 at 12:26
  • @Gerry85 No it is not a snippet. It doesn't compile! Regardless, it doesn't demonstrate the problem you have claimed, so your problem is somewhere else. – RB. Apr 03 '13 at 12:27
  • What this does it take in tags from ahml email and store information relating to them in a DB before scheduling them to send to the MTA. However, it thinks that having a+ is invalid and is recording it as a BAD ADDRESS in the db. – Gerry85 Apr 03 '13 at 12:33
  • @Gerry85, if 'a+' fails in your application then it isn't because of the code you presented (aside from the syntax errors). Are you sure the error isn't coming from somewhere else? – Jesper Fyhr Knudsen Apr 03 '13 at 12:35
  • I may have to dig a little but in 98% sure its from this location. – Gerry85 Apr 03 '13 at 12:54

2 Answers2

1

I'm not sure where the problem is (I assume it isn't the obvious syntax errors) The code you presented does work the way you describe you'd like it to.

I rewrote it a little so it compile and work:

public static bool IsValidEmail(string sEmail)
{
    return sEmail.IndexOf("@") != -1 && sEmail.IndexOf(".") != -1;
}

Validated with this nunit test:

[Test]
public void IsValidEmailTest()
{
    Assert.IsTrue( IsValidEmail( "name+@domain.com" ) );
    Assert.IsTrue( IsValidEmail( "name@domain.com" ) );
    Assert.IsFalse( IsValidEmail( "namedomain.com" ) );
    Assert.IsFalse( IsValidEmail( "namedomaincom" ) );
    Assert.IsFalse( IsValidEmail( "named@omaincom" ) );
}
Jesper Fyhr Knudsen
  • 7,802
  • 2
  • 35
  • 46
  • unfortunately this does not solve the case, im starting to think that the issue may arise else where i the application – Gerry85 Apr 03 '13 at 13:05
  • @Gerry85, it does sound like you have to look elsewhere, the code I posted work, as proved by the unit test. Good luck. – Jesper Fyhr Knudsen Apr 03 '13 at 13:07
  • I think im going to need it, this was written 4 years ago by someone no longer here. Little or no annotation to work on either. – Gerry85 Apr 03 '13 at 13:19
  • When i tried what you have above i have an issue with 'void' - Expected class, delegate, enum, interface or struct – Gerry85 Apr 03 '13 at 14:20
  • @Gerry85, what did you write? notice the method you wrote returns `bool` but the nunit test is a `void`, I only added the test, to show what input it handles. – Jesper Fyhr Knudsen Apr 03 '13 at 15:24
  • Im serously just winging this, i havent a C# background. However the above now does work. I wasnt able to package based on a school boy error, the .net framework. – Gerry85 Apr 04 '13 at 13:23
  • @Gerry85, good to hear you found a solution. – Jesper Fyhr Knudsen Apr 04 '13 at 18:26
0

Try this solution:

    protected override bool EvaluateIsValid()
    {
        string val = this.GetControlValidationValue(this.ControlToValidate);
        string pattern = @"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z][a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$";
        Match match = Regex.Match(val.Trim(), pattern, RegexOptions.IgnoreCase);

        if (match.Success)
            return true;
        else
            return false;
    }
Mennan
  • 4,451
  • 13
  • 54
  • 86