ulhas_c
ulhas_c
Reputation Top 1%
ulhas_c
13 Snippets  (64th place)
Published
1 Channel
Created
1 Channel
Following
340 points  (90th place)
Reputation
Top 10% Publisher
Junior Code Generator
Serious Code Generator
Senior Code Generator
Junior Publisher
Serious Publisher
Junior Topic Hub
Junior Trend Maker
Serious Trend Maker
Junior Popular Coder
Serious Popular Coder
Senior Popular Coder
Junior Autobiographer
Serious Autobiographer
Senior Famous Coder
Junior Wise Coder

Recent Snippets See all snippets by ulhas_c

public by ulhas_c  1940  1  5  0

PropertiesMustMatchAttribute

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace EBS.Recruitment.BL.Helpers.Validation
{
    //from: http://byatool.com/mvc/custom-data-annotations-with-mvc-how-to-check-multiple-properties-at-one-time/
    /// <summary>
    /// Properties must match. Applied to a class
    /// </summary>
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
    public class PropertiesMustMatchAttribute : ValidationAttribute
    {
        /// <summary>
        /// Gets or sets the name of the first property.
        /// </summary>
        /// <value>The first name of the property.</value>
        public String FirstPropertyName { get; set; }
        /// <summary>
        /// Gets or sets the name of the second property.
        /// </summary>
        /// <value>The name of the second property.</value>
        public String SecondPropertyName { get; set; }

        //Constructor to take in the property names that are supposed to be checked
        /// <summary>
        /// Initializes a new instance of the <see cref="PropertiesMustMatchAttribute"/> class.
        /// </summary>
        /// <param name="firstPropertyName">Name of the first property.</param>
        /// <param name="secondPropertyName">Name of the second property.</param>
        public PropertiesMustMatchAttribute(String firstPropertyName, String secondPropertyName)
        {
            FirstPropertyName = firstPropertyName;
            SecondPropertyName = secondPropertyName;
            if (ErrorMessage.IsNullOrEmpty())
                ErrorMessage = "The fields failed validation.";
        }

        /// <summary>
        /// Determines whether the specified value is valid.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>
        /// 	<c>true</c> if the specified value is valid; otherwise, <c>false</c>.
        /// </returns>
        public override Boolean IsValid(Object value)
        {
            Type objectType = value.GetType();
            //Get the property info for the object passed in.  This is the class the attribute is
            //  attached to
            //I would suggest caching this part... at least the PropertyInfo[]
            PropertyInfo[] neededProperties =
            objectType.GetProperties()
            .Where(propertyInfo => propertyInfo.Name == FirstPropertyName || propertyInfo.Name == SecondPropertyName)
            .ToArray();

            if (neededProperties.Count() != 2)
            {
                throw new ApplicationException("PropertiesMustMatchAttribute error on " + objectType.Name);
            }

            Boolean isValid = true;

            //Convert both values to string and compare...  Probably could be done better than this
            //  but let's not get bogged down with how dumb I am.  We should be concerned about
            //  dumb you are, jerkface.
            if (!Convert.ToString(neededProperties[0].GetValue(value, null)).Equals(Convert.ToString(neededProperties[1].GetValue(value, null))))
            {
                isValid = false;
            }

            return isValid;
        }
    }
}
;

public by ulhas_c  1840  1  5  0

MinimumValueAttribute

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EBS.Recruitment.BL.Helpers.Validation
{
    /// <summary>
    /// Requires the value of the property to be greater than or equal to a certain minimum value.
    /// </summary>
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
    public class MinimumValueAttribute : RangeAttribute
    {
        public MinimumValueAttribute(double minimum)
            : base(minimum, double.MaxValue)
        {
            if (ErrorMessage.IsNullOrEmpty())
            {
                ErrorMessage = "The value must be greater than or equal to " + minimum;
            }
        }
    }
}
;

public by ulhas_c  1738  1  5  0

MaximumValueAttribute

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EBS.Recruitment.BL.Helpers.Validation
{
    /// <summary>
    /// Requires the value of the property to be less than or equal to a certain maximum value.
    /// </summary>
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
    public class MaximumValueAttribute : RangeAttribute
    {
        public MaximumValueAttribute(double maximum)
            : base(double.MinValue, maximum)
        {
            if (ErrorMessage.IsNullOrEmpty())
            {
                ErrorMessage = "The value must be less than or equal to " + maximum;
            }
        }
    }
}
;

public by ulhas_c  1679  1  5  -1

IsValidEmailAddressAttribute

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace EBS.Recruitment.BL.Helpers.Validation
{
    /// <summary>
    /// Checks that the field/property is a valid email address.
    /// </summary>
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
    public class IsValidEmailAddressAttribute : ValidationAttribute
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="IsValidEmailAddressAttribute"/> class.
        /// </summary>
        public IsValidEmailAddressAttribute()
            : base()
        {
            if (ErrorMessage.IsNullOrEmpty())
                ErrorMessage = "Please enter a valid email address.";
        }
        /// <summary>
        /// Determines whether the specified value is valid.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>
        /// 	<c>true</c> if the specified value is valid; otherwise, <c>false</c>.
        /// </returns>
        public override bool IsValid(object value)
        {
            if (value == null) //we don't care if it's required or not.
            {
                return true;
            }
            var address = (string)value;
            try
            {
                var addressMail = new MailAddress(address);
                addressMail = null;
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
;

public by ulhas_c  1763  2  5  0

DateOnlyAttribute

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EBS.Recruitment.BL.Helpers.Validation
{
    //from: https://gist.github.com/esouto/3176852
    /// <summary>
    /// Must be Date
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
    public class DateOnlyAttribute : ValidationAttribute
    {
        public DateOnlyAttribute() :
            base("\"{0}\" must be a date without time portion.")
        {
        }

        public override bool IsValid(object value)
        {
            if (value != null)
            {
                if (value.GetType() == typeof(DateTime))
                {
                    DateTime dateTime = (DateTime)value;
                    return dateTime.TimeOfDay == TimeSpan.Zero;
                }
                else if (value.GetType() == typeof(Nullable<DateTime>))
                {
                    DateTime? dateTime = (DateTime?)value;
                    return !dateTime.HasValue
                        || dateTime.Value.TimeOfDay == TimeSpan.Zero;
                }
            }
            return true;
        }

        public override string FormatErrorMessage(string name)
        {
            return string.Format(CultureInfo.CurrentCulture,
                ErrorMessageString, name);
        }
    }
}
;