Essayflow goes over the full essay creating procedure - from organizing and likely to conclusion. Composing essays is organised gradually so i highly recommend that you use it this way. On the other hand, you will see coming from the sidebar that this guideline is split into several help me write an essay that it is separated into reduced parts or subsections. Allowing you to often study it right thru from beginning to end or go directly to the area that’s most relevant to you. You will notice in the topbar choices higher than that additionally there is a glossary of terms employed in this article; and a list of encouraged additionally reading through and internet based information. It can do not deliver comprehensive advice on typical study capabilities although it does deal with some areas of reading for crafting and the way compose a literature assessment. Not like some instructions, that one does not have everything to say about using computing devices with the exception of: make use of them, and keep your operate often.

ASP.NET MVC 2 Model Validation

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}$") {}
    }
}

This entry was posted on Monday, February 15th, 2010 at 12:40 pm and is filed under MVC. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

5 Responses to “ASP.NET MVC 2 Model Validation”

Gerard February 16th, 2010 at 10:45 am

Hi,

How to localize your data validation message?

Thanks,

Mike February 16th, 2010 at 11:51 am

@GERARD Hey this is a good question! I haven’t worked on that yet, but I don’t have any project at the moment, so I’ll be working on that today. You should see a post about it today or tomorrow, if all goes well!

khan March 2nd, 2010 at 2:50 pm

Hi, great example, but how do you run off the default stuff mvc adds to the model like value “abc” is not valid, even though this is not part of the validation framework you use. The problem is the messages are too generic and it would be nicer to customize them. Even though the message in the framework you use does show up if you enter “abc” in an in field it displays a message you have not added yourself.

Thanks

Mumtaz Ali June 16th, 2010 at 9:58 am

Hi,

Above code is not working here,

I have placed the EmailAttribute in same project where I placed my Entity classes, but UI project id different, here directly using RegularExpression is working but not custom class.

Please help me.

Thanks.

Leave a Reply