Skip to content

Instantly share code, notes, and snippets.

View elizabeth-young's full-sized avatar

Elizabeth Fairbairn elizabeth-young

View GitHub Profile
@elizabeth-young
elizabeth-young / ModelExtensions.cs
Created August 10, 2016 07:46
Mvc ModelState extensions to get error list from ModelState
using System.Collections.Generic;
using System.Linq;
usingusing System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
namespace Gists.Mvc.Helpers
{
public static class ModelExtensions
{
@elizabeth-young
elizabeth-young / AddGulpToMSBuild.md
Last active January 5, 2016 15:59
Add gulp to MS Build

To run gulp on build

Add the following nuget packages

https://github.com/kmees/MSBuild.NodeTools

Install-Package MSBuild.Npm
Install-Package MSBuild.Gulp

Add tasks for each project configuration to gulpfile.js

gulp.task('build-Debug', function () {
@elizabeth-young
elizabeth-young / RegularExpressions.cs
Last active January 6, 2016 08:04
Some regular expressions - postcode, email, phone & mobile
public static class Regexes
{
public const string Postcode = @"(GIR\s0AA)|(gir\s0aa)|((([A-PR-UWYZa-pr-uwyz][0-9][0-9]?)|(([A-PR-UWYZa-pr-uwyz][A-HK-Ya-hk-y][0-9][0-9]?)|(([A-PR-UWYZa-pr-uwyz][0-9][A-HJKSTUWa-hjkstuw])|([A-PR-UWYZa-pr-uwyz][A-HK-Ya-hk-y][0-9][ABEHMNPRVWXYabehmnprvwxy]))))\s{0,1}[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2})";
public const string Email = @"^([a-zA-Z0-9_\-\.]+)@{1}((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,6}|[0-9]{1,3})$";
public const string Mobile = @"^(((\+?44\s?[127]\d{2,3}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+?44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+?44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))$";
}
@elizabeth-young
elizabeth-young / directives.js
Created July 1, 2013 09:17
Some Angular validation directives
'use strict';
var directives = angular.module('app', []);
// override the default input to update on blur
directives.directive('input', function () {
return {
restrict: 'E',
require: 'ngModel',
link: function (scope, elm, attr, ngModelCtrl) {
if (attr.type === 'radio' || attr.type === 'checkbox') return;
@elizabeth-young
elizabeth-young / MinDigitsIncluded.cs
Last active September 25, 2017 01:34
Validation attribute for ensuring a minimum number of digits entered
public class MinDigitsIncluded : ValidationAttribute, IClientValidatable
{
private const string DefaultErrorMessage = "{0} must contain at least {1} digits.";
public int MinDigits { get; private set; }
public MinDigitsIncluded(int minDigits) : base(DefaultErrorMessage)
{
MinDigits = minDigits;
}
@elizabeth-young
elizabeth-young / DateGreaterThanAttribute.cs
Last active September 25, 2017 01:54
Validation attribute for testing a data is later than another
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class DateGreaterThanAttribute : ValidationAttribute, IClientValidatable
{
private const string DefaultErrorMessageFormatString = "The {0} field is required.";
public string OtherProperty { get; private set; }
public DateGreaterThanAttribute(string otherProperty)
{
if (string.IsNullOrEmpty(otherProperty))
{
@elizabeth-young
elizabeth-young / RequiredIfAttribute.cs
Last active January 6, 2016 08:05
Validation attribute for required value if another property is set
public enum Comparison
{
IsEqualTo,
IsNotEqualTo
}
/// <summary>
/// Validation attribute for required value if another property is set
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public sealed class RequiredIfAttribute : ValidationAttribute, IClientValidatable
@elizabeth-young
elizabeth-young / RegularExpressionIfAttribute.cs
Last active January 6, 2016 08:05
Validation attribute for regular expression validating property if another property is set
public enum Comparison
{
IsEqualTo,
IsNotEqualTo
}
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public sealed class RegularExpressionIfAttribute : ValidationAttribute, IClientValidatable
{
public string RegEx { get; private set; }
@elizabeth-young
elizabeth-young / CollectionMinMaxLengthValidationAttribute.cs
Last active January 6, 2016 08:05
Validation attribute for ensuring a collection has a minimum and maximum length
public class CollectionMinMaxLengthValidationAttribute : ValidationAttribute
{
const string errorMessage = "{0} must contain at least {1} item(s).";
const string errorMessageWithMax = "{0} must contain between {1} and {2} item(s).";
int minLength;
int? maxLength;
public CollectionMinMaxLengthValidationAttribute(int min)
{
minLength = min;
@elizabeth-young
elizabeth-young / BaseController.cs
Last active January 6, 2016 08:05
Base MVC Controller using cultures - to be used with LocalisationHelper
public abstract partial class BaseController : Controller
{
private const string _langCookie = "lang";
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
SetCurrentCulture();
base.OnActionExecuting(filterContext);
}