Skip to content

Instantly share code, notes, and snippets.

@Dynyx
Dynyx / _layout.cshtml
Created June 4, 2012 12:56
Default _layout.cshtml
<!--
Requires the following Nuget packages:
1. MVCFlash
2. JQueryUI (combined)
3. Fancybox
-->
<!DOCTYPE html>
<html>
<head>
<title>@ViewBag.Title</title>
@Dynyx
Dynyx / SQLDateSnippets.sql
Created June 4, 2012 12:57
Handy SQL date snippets
---Today
SELECT GETDATE() 'Today'
----Yesterday
SELECT DATEADD(d,-1,GETDATE()) 'Yesterday'
----First Day of Current Week
SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),0) 'First Day of Current Week'
----Last Day of Current Week
@Dynyx
Dynyx / ResetTableIdentity.sql
Created June 4, 2012 12:58
Reset table identity
--Reset identity to 0
DBCC CHECKIDENT('TableName', RESEED, 0)
@Dynyx
Dynyx / HandyRegexExpressions.txt
Created June 4, 2012 13:00
Handy regex expressions
//replace all but links
/<(?!\/?a(?=>|\s.*>))\/?.*?>/g
//validate url
/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \?=.-]*)*\/?$/
//Validate US phone number
/^(\+\d)*\s*(\(\d{3}\)\s*)*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$/
//Test if a password is strong
@Dynyx
Dynyx / InsertHTMLTinyMCE.html
Created June 4, 2012 13:01
Insert HTML into a tinyMCE editor at the cursor location
<a href="javascript:void(0);" onclick="tinyMCE.execCommand('mceInsertContent', false, '<a href=\'http://www.google.com\'>This is a test</a>');">Insert</a>
@Dynyx
Dynyx / ValidatePhone.cs
Created June 4, 2012 13:02
Phone number validation
/// <summary>
/// class for validating a phone number in our MVC Model inheriting from the ValidationAttribute. We mark it with the
/// AttributeUsageAttribute <see cref="http://msdn.microsoft.com/en-us/library/tw5zxet9(v=VS.100).aspx"/> To
/// tell the compiler that it can only be used on properties or fields
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
sealed public class PhoneNumberFormatAttribute : ValidationAttribute
{
/// <summary>
/// our format value
@Dynyx
Dynyx / EventLog.cs
Created June 4, 2012 13:03
Log exceptions to the event log
public static void LogException(string ErrorDescription)
{
// The name of our log in the event logs
string Log = “AspNetError”;
// Check to see fi the log for AspNetError exists on the machine
// If not, create it
if ((!(EventLog.SourceExists(Log))))
{
@Dynyx
Dynyx / ExceptionLogging.cs
Created June 4, 2012 13:03
Logging exceptions
// The Logger Class
public static class Logger
{
public static void Log(string message)
{
File.AppendAllText("C:\\MyLogFile.txt", DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + ":\t" + message + Environment.NewLine);
}
}
// Usage
@Dynyx
Dynyx / ConvertStringToListWord.cs
Created June 4, 2012 13:04
Convert a string to a List of words
private static List<string> stringToWordArray(string s)
{
List<string> words = new List<string>();
string word;
s = s.ToUpper();
s = s.Trim(',', '.', '"', '-', ' ', ':', ';', '\n', '!', '?');
while (s.Length > 0)
{
if (s.Contains(' '))
{
@Dynyx
Dynyx / PostalCodeLookup.cs
Created June 4, 2012 13:05
Postal code lookup
public static class PostalLookup
{
public static Postal Find(string countryCode, string postalCode)
{
// remove letters from postal code
postalCode = Regex.Replace(postalCode, "[^0-9]", "");
if (string.IsNullOrEmpty(postalCode) || postalCode.Length != 4)
return null;