0% found this document useful (0 votes)
25 views

Using System

This document defines an EmailAttribute class that validates email addresses. It inherits from the ValidationAttribute class and overrides the IsValid method to check email addresses against a regular expression. The regex pattern comes from researching common email validation patterns. The constructor initializes a Regex instance with this pattern. IsValid converts the value to a string, skips empty values, and checks for an exact match against the regex.

Uploaded by

Ulka Bane Bendre
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Using System

This document defines an EmailAttribute class that validates email addresses. It inherits from the ValidationAttribute class and overrides the IsValid method to check email addresses against a regular expression. The regex pattern comes from researching common email validation patterns. The constructor initializes a Regex instance with this pattern. IsValid converts the value to a string, skips empty values, and checks for an exact match against the regex.

Uploaded by

Ulka Bane Bendre
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

using

System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Text.RegularExpressions;
  
namespace Validation
{
    /// <summary>
    /// Email Attribute class used for
email validation. Used similar to the
System.ComponentModel.DataAnnotations.Regul
arExpressionAttribute.
    /// </summary>
    public class EmailAttribute :
ValidationAttribute
    {
        #region Properties
  
        /// <summary>
        /// Gets or sets the Regular
expression.
        /// </summary>
        /// <value>The regex.</value>
        private Regex Regex { get; set; }
  
        /// <summary>
        /// Gets the pattern used for email
validation.
        /// </summary>
        /// <value>The pattern used for
email validation.</value>
        /// <remarks>
        /// Regular Expression Source -
Comparing E-mail Address Validating Regular
Expressions
        /// <see
cref="http://fightingforalostcause.net/misc
/2006/compare-email-regex.php"/>
        /// </remarks>
        public string Pattern
        {
            get
            {
                return
                    @"^([\w\!\#$\%\&\'\*\
+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\
+\-\/\=\?\^\`{\|\}\~]+@((((([a-zA-Z0-9]{1}
[a-zA-Z0-9\-]{0,62}[a-zA-Z0-9]{1})|[a-zA-
Z])\.)+[a-zA-Z]{2,6})|(\d{1,3}\.){3}\d{1,3}
(\:\d{1,5})?)$";
            }
        }
  
        #endregion Properties
  
        #region Ctors
  
        /// <summary>
        /// Initializes a new instance of
the <see cref="EmailAttribute"/> class.
        /// </summary>
        public EmailAttribute()
        {
            this.Regex = new
Regex(this.Pattern);
        }
  
        #endregion Ctors
  
        /// <summary>
        /// Determines whether the
specified value of the object is valid.
        /// </summary>
        /// <param name="value">The value
of the object to validate.</param>
        /// <returns>
        /// true if the specified value is
valid; otherwise, false.
        /// </returns>
        public override bool IsValid(object
value)
        {
            // convert the value to a
string
            var stringValue =
Convert.ToString(value,
CultureInfo.CurrentCulture);
  
            // automatically pass if value
is null or empty. RequiredAttribute should
be used to assert an empty value.
            if
(string.IsNullOrWhiteSpace(stringValue))
return true;
  
            var m =
Regex.Match(stringValue);
  
            // looking for an exact match,
not just a search hit.
            return (m.Success && (m.Index
== 0) && (m.Length == stringValue.Length));
        }
    }
}

You might also like