Statistics
| Branch: | Revision:

root / trunk / hammock / src / net35 / Hammock / Validation / ValidEmailSpecification.cs @ 0eea575a

History | View | Annotate | Download (1.2 kB)

1
using System.Text.RegularExpressions;
2
using Hammock.Specifications;
3

    
4
namespace Hammock.Validation
5
{
6
    public class ValidEmailSpecification : HammockSpecification<string>
7
    {
8
        // Accepts names, i.e. John Smith <john@johnsmith.com>
9
        private static readonly Regex _names =
10
            new Regex(
11
                @"\w*<([-_a-z0-9'+*$^&%=~!?{}]+(?:\.[-_a-z0-9'+*$^&%=~!?{}]+)*@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d+)?)>",
12
#if !SL4 && !MonoTouch
13
                RegexOptions.Compiled |
14
#endif
15
                 RegexOptions.IgnoreCase
16
                );
17

    
18
        // Just an email address
19
        private static readonly Regex _explicit =
20
            new Regex(
21
                @"^[-_a-z0-9'+*$^&%=~!?{}]+(?:\.[-_a-z0-9'+*$^&%=~!?{}]+)*@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d+)?$",
22
#if !SL4 && !MonoTouch
23
                RegexOptions.Compiled |
24
#endif
25
                 RegexOptions.IgnoreCase
26
                );
27

    
28
        public override bool IsSatisfiedBy(string instance)
29
        {
30
            var result = _explicit.IsMatch(instance) || _names.IsMatch(instance);
31

    
32
            return result;
33
        }
34
    }
35
}