Validation in MVC

Range Validation in MVC

The Range attribute specifies minimum and maximum constraints for a numerical value. If the Music Store only wanted to serve middle-aged customers, you could add an Age property to the Order class and use the Range attribute as in the following code:

public int Age { get; set; }

 The first parameter to the attribute is the minimum value, and the second parameter is the maximum value. The values are inclusive. The Range attribute can work with integers and doubles, and another overloaded version of the constructor will take a Type parameter and two strings (which can allow you to add a range to date and decimal properties, for example).

[Range(typeof(decimal), "0.00", "49.99")]
public decimal Price { get; set; }

RegularExpression Validation in MVC

Some properties of Order require more than a simple presence or length check. For example, you’d like to ensure the Email property of an Order contains a valid, working e-mail address. Unfortunately, it’s practically impossible to ensure an e-mail address is working without sending a mail message and waiting for a response. What you can do instead is ensure the value looks like a working e-mail address using a regular expression:

public string Email { get; set; }

Regular expressions are an effi cient and terse means to enforce the shape and contents of a string value. If the customer gives you an e-mail address and the regular expression doesn’t think the string looks like an e-mail address, the customer will see the error in Figure.

StringLength Validation in MVC

You’ve forced the customer to enter his name, but what happens if he enters a name of enormous length? Wikipedia says the longest name ever used belonged to a German typesetter who lived in Philadelphia. His full name is more than 500 characters long. Although the .NET string type can store (in theory) gigabytes of Unicode characters, the MVC Music Store database schema sets the maximum length for a name at 160 characters. If you try to insert a larger name into the database, you’ll have an exception on your hands. The StringLength attribute can ensure the string value provided by the customer will fit in the database:

public string FirstName { get; set; }
public string LastName { get; set; }

Notice how you can stack multiple validation attributes on a single property. With the attribute in place, if a customer enters too many characters, he’ll see the default error message shown below the LastName field in Figure.


MinimumLength is an optional, named parameter you can use to specify the minimum length for a string. The following code requires the FirstName property to contain a string with three or more characters (and less than or equal to 160 characters) to pass validation:

[StringLength(160, MinimumLength=3)]
public string FirstName { get; set; }

Required Validation in MVC

Because you need the customer to give you his fi rst and last name, you can decorate the FirstName and LastName properties of the Order model with the Required attribute:

public string FirstName { get; set; }
public string LastName { get; set; }
[Required(ErrorMessage = "Nick name is required")]
public string NickName { get; set; }

 The attribute raises a validation error if either property value is null or empty.

Like all the built-in validation attributes, the Required attribute delivers both server-side and client side validation logic (although internally, it is another, different component in the MVC framework that delivers the client-side validation logic for the attribute through a validation adapter design).

With the attribute in place, if the customer tries to submit the form without providing a last name, he’ll see the default error in Figure.


However, even if the customer does not have JavaScript enabled in his browser, the validation logic will catch an empty name property on the server, too.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s