Deborah's Developer MindScape

         Tips and Techniques for Web and .NET developers.

May 1, 2011

Silverlight/WCF RIA Single Property Validation

Filed under: ASP.NET RIA Services,C#,Silverlight @ 9:01 pm

When you identify the validation scenarios for your application, you will find that some only require basic single property validation. Examples:

  • Ensure that a first name is entered.
  • Ensure that the first name is not longer than 20 characters
  • Ensure that the age is between 16 and 120.
  • Ensure that the email address is valid.

NOTE: This post is part of a set of validation posts which starts here and it uses the sample code from this prior post.

Each of these requirements can be handled by Silverlight’s single field validation using property attributes.

When you use WCF RIA Services and your own set of business objects, these attributes are set in the business layer on the business object properties. WCF RIA Services then automatically generates a copy of your business object properties with their attributes on the client for use with Silverlight.

Silverlight performs the validation when the user leaves the field AND before the data is submitted to the server. If there is a validation error, the data is not submitted to the server and a validation error appears.

Each of the simple validation attributes are discussed below.

Required Field Validation

Use the Required attribute to mark a property as required. For example, if the first name is required, the first name property is adorned as follows:

public string FirstName { get; set; }

If you make this change and run the sample application (from this prior post), the first name field is automatically validated. Clear the field and you will see this:


Silverlight’s built-in validation automatically kicks in and you see the default required validation error message.

NOTE: You can modify the Silverlight XAML to specify the ValidatesOnNotifyDataErrors property as part of the binding expression. However, this is not necessary. The default value of this property is true, so the validation errors are reported by default.

If you don’t like the error message, you can change it by setting one of the named parameters of the Required attribute:

[Required(ErrorMessage="Please enter a first name.")]
public string FirstName { get; set; }

The message then appears as shown below:


Use required field validation any time you want to ensure that the user enters a value into a Silverlight control.

String Length Validation

Often times the data entered on a page in Silverlight will get stored to a database where a maximum field size is defined. It is helpful for the users to be aware of this maximum before attempting to submit the data to the server.

For example, if the first name is set to a maximum of 20 characters in the database, you can set the StringLength attribute as follows:

[Required(ErrorMessage="Please enter a first name.")]
[StringLength(20, ErrorMessage = "First name cannot exceed 20 characters.")]
public string FirstName { get; set; }

Use string length validation any time you want to ensure that the user does not enter more data than can be stored in the database table or any other time that a string length cannot exceed a maximum value.

Range Validation

When entering numbers, often times the value must be limited to a specific range of values. For example, in this application the age must be between 16 and 120.

[Range(16, 120,
ErrorMessage="Please enter your age. You must be 16 or older.")]
public int Age { get; set; }

This results in the following:


NOTE: The Range attribute has overloads with parameters that are integers or doubles. To validate other types (such as decimal), you need to use the Range overload that takes a type as the first parameter and string values to convert to the defined type. For example:

[Range(typeof(Decimal),"0", "40000")]
public decimal TuitionPaid{ get; set; }

Use range validation any time you want to validate user entry to a range of numeric values.

Regular Expression Validation

Regular expression validation allows you to perform pattern matching on the entered value. For example, an email address must match a specific pattern with a local part of the address followed by an at sign (@) followed by a domain name. The Regular Expression attribute could look like this:

[Regular(@"^(?("")("".+?""@)|" +
      @"(([0-9a-zA-Z]((\.(?!\.))|" +
      @"[-!#\$%&’\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))" +
      @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|" +
      ErrorMessage="Please enter a valid email address")]

public string Email { get; set; }

NOTE: Replace "Regular" above with "RegularExpression". For whatever reason, the entire string was blocked in this post if the word "Expression" appeared above.

Other properties that could use a regular expression include phone numbers, social security numbers, and any other entry that must match a specific pattern.

You can find more information on regular expressions here.

Use regular expressions any time you want to validate a user’s entry based on pattern matching.

For more complex validation scenarios, see this prior post.


EDIT 5/2/11: Corrected typographical errors. Corrected the regular expression, which did not paste into the post correctly.


  1.   Hayle — June 29, 2011 @ 1:20 pm    Reply

    Whoa, things just got a whole lot eaiesr.

  2.   Michael Ma — October 10, 2011 @ 3:44 pm    Reply

    Hi there, I’m trying to start Silverlight and WCF RIA, this is a very good sample for me to get started. Could you send me the sample source code please? My email address:
    Thanks a lot.

  3.   mike — January 11, 2012 @ 7:32 am    Reply

    i made it like in the example above. but it does not work for me. it seems that the binding does not work and if i type something in the textbox the setter is not hit of the property.
    Anybody an idea what the problem might be?

    Thanks & with regards

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2023 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs