1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.Components
.Validator
18 using System
.Collections
;
21 /// Validate that this is a valid (formatted) email using regex
24 public class EmailValidator
: RegularExpressionValidator
27 /// From http://www.codeproject.com/aspnet/Valid_Email_Addresses.asp
29 private static readonly String emailRule
= @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
30 @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
31 @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
34 /// Initializes a new instance of the <see cref="EmailValidator"/> class.
36 public EmailValidator() : base(emailRule
)
41 /// Gets a value indicating whether [supports browser validation].
44 /// <c>true</c> if [supports browser validation]; otherwise, <c>false</c>.
46 public override bool SupportsBrowserValidation
52 /// Applies the browser validation.
54 /// <param name="config">The config.</param>
55 /// <param name="inputType">Type of the input.</param>
56 /// <param name="generator">The generator.</param>
57 /// <param name="attributes">The attributes.</param>
58 /// <param name="target">The target.</param>
59 public override void ApplyBrowserValidation(BrowserValidationConfiguration config
, InputElementType inputType
,
60 IBrowserValidationGenerator generator
, IDictionary attributes
,
63 generator
.SetEmail(target
, BuildErrorMessage());
67 /// Returns the key used to internationalize error messages
70 protected override string MessageKey
72 get { return MessageConstants.InvalidEmailMessage; }