Thursday, March 26, 2009

Data Validation On SilverLight 3.0

Silverlight supports data validation in TwoWay bindings for target-to-source updates.  To setting the following two properties ValidatesOnExceptions=True,NotifyOnValidationError=True  to True we can receive  Error Notification from Binding engine. 

Note:  Siliverlight throws Exception in two cases 

  1.  Exceptions thrown from the binding engine's type converter.
  2. Exceptions thrown from the binding object's set accessor.

Setting ValidatesOnExceptions to true tells the binding engine to create a validation error when an exception occurs.

Setting NotifyOnValidationError to true tells the binding engine to raise the BindingValidationError event when a validation error occurs and resolves.

The BindingValidationError event is a routed event, so if you do not handle it on the element that raised the event, it will continue to bubble up until it is handled.

Here is sample Program.  Here i going to build a small data entry form which requires user to enter  Name , Age, Email and Phone.  in this  Data entry form I am setting Name,Email, age are  Required fields and   Age is numeric if you enter more than 100 it will throw an Expectation saying Invalid Input. No validation set on Phone Property . all the above mentioned  properties makes a class called Contacts  as shown below.



Now i am Initializing the Contacts class properties with the following values as shown.


All Inputs field has valid data.


This form shows Name can not be empty.


This Data entry form shows  Age can not be more than 100 .




1 comment:

moonstruck said...

the article helped a lot , thank-you very much . But the validation doesn't work without default values. :(
Can you please put up an article on validating e-mail addresses with regular expressions.
Thanx a lot again.