ASP.NET MVC 2 Model Validation

February 15th, 2010 / 5 Comments » / by Mike

In projects I usually work on, we always use a data validation framework. But now in ASP.NET MVC 2 you can do it just as you would using a framework.

You need to write your model with the proper validation attributes.

using System;
using System.ComponentModel.DataAnnotations;

namespace FunWithMvc2RC2
{
    public class Test
    {
        // StringLenght
        [StringLength(5, ErrorMessage = "Maximum 25 Characters")]
        public string StringLength { get; set; }

        // Required
        [Required(ErrorMessage = "Required Field")]
        public string Required { get; set; }

        // Required and StringLenght
        [Required(ErrorMessage = "Required Field")]
        [StringLength(5, ErrorMessage = "Maximum 25 Characters")]
        public string Combos { get; set; }

        // Range Attribute
        [Range(1, 31, ErrorMessage = "Minimum 1; Maximum 31")]
        public int Range { get; set; }

        // RegularExpression Attribute
        [RegularExpression(@"^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$", ErrorMessage = "Invalid Email Address")]
        public string Regex { get; set; }

        // Custom Regular Expression EmailAttribute
        [Email(ErrorMessage = "Email Validation")]
        public string Email { get; set; }
    }
}

This will give you something like this:

And to make the Email Attribute you simply make a new class, which inherits RegularExpressionAttribute and then send the constructor an Email Address Regex! It’s this simple. You could do the same for any other Regular Expression based validation. Just Make sure you call your class – SomethingAttribute.

using System;
using System.ComponentModel.DataAnnotations;

namespace FunWithMvc2RC2
{
    public class EmailAttribute : RegularExpressionAttribute
    {
        public EmailAttribute() :
            base(@"^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$") {}
    }
}

HOW TO: Turn Off Google Buzz

February 15th, 2010 / No Comments » / by Mike

If you’re like me and you don’t really like Google Buzz, know that you can easily turn it off by scrolling at the bottom of any page inside GMAIL.

Yeah, it’s that easy…

“Edit WCF Configuration” is missing from Visual Studio 2008 context menu

February 4th, 2010 / 2 Comments » / by Mike

So this morning I format a laptop and install a fresh copy of Visual Studio 2008. I then try to edit the Web.Config using Edit WCF Configuration, but wait it’s missing! What the hell?

So I look around in VS menus and find Tools -> WCF Service Configurator Editor. I clicked it and modified my Web.Config…But I really liked to have the option to just open this in the Context Menu! I clicked again just to humor myself and it was back!

I don’t know what happened there, it’s actually the first time I see this, but I don’t play much with WCF! I was actually able to reproduce this first hand on a virtual machine….