Silverlight / WPF - Email Validation in silverlight XAML

Asked By Ramachandran on 07-Nov-11 04:51 AM
How to do a Email Validation in Silverlight Xaml in MVVM pattern using C #. plse help me with design and code examples
Reena Jain replied to Ramachandran on 07-Nov-11 07:51 AM
Hi,

A ValidatioRule in a BindingExpression only triggers, when the target side of the binding changes. If you want to set a validation error by code you can use the following snippet.
ValidationError validationError =
  new ValidationError(regexValidationRule,
  textBox.GetBindingExpression(TextBox.TextProperty));
   
validationError.ErrorContent = "This is not a valid e-mail address";
   
Validation.MarkInvalid(
  textBox.GetBindingExpression(TextBox.TextProperty),
  validationError);

Use ClearInvalid property of validation to clear the error

Validation.ClearInvalid(textBox.GetBindingExpression(TextBox.TextProperty));

Hope this will help you
Robbe Morris replied to Ramachandran on 07-Nov-11 08:09 AM
Search on IValueConverter Email Regex in Google.  Those examples will show you how to bind the validator to your TextBox.
Riley K replied to Ramachandran on 08-Nov-11 12:16 AM


See how to use INotifyDataErrorInfo in an MVVM manner

for example

[Required(ErrorMessage = "Please provide an email address.")]
  [RegularExpression(@"^[\w\.=-]+@[\w\.-]+\.[\w]{2,3}$", ErrorMessage = "Please provide a valid email address.")]
  public string Email
  {
    get { return _email; }
    set
    {
      _email = value;
      NotifyPropertyChanged(() => Email);
    }
  }

Refer this link

http://www.emileinarsson.se/silverlight-4-mvvm-validation-inotifydataerrorinfo/

Regards
Kirtan Patel replied to Ramachandran on 17-Nov-11 11:48 AM
check out this good article that explain about validations in silver light 

http://geekswithblogs.net/PeterTweed/archive/2009/10/12/silverlight--easy-data-validation.aspx

Thanks
s j replied to Ramachandran on 18-Nov-11 04:37 AM

Hi,

Hope this below site is useful for u.
http://www.vectorlight.net/silverlight/tutorials/validation_in_silverlight.aspx

Let me know if u have other queries regarding this.



Cheers,
Samantha.