Some time it is business requirement to temporarily suspend validation, recently I come across this situation so as all developer do I took the lazy way and try to find solution in web but I didn't find any suitable solution so I have decided to write my own. You can download the complete source code form hear.
As you can see if the rule is active then it will simply return a ValidationResult so everything will be as though as you didn’t have validation rules in xaml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Windows.Controls;
namespace EnableDisableValidation.ValidationRules
{
public class ValidateContactNumber : ValidationRule
{
public ValidateContactNumber()
{
this.IsValidate = true;
}
/// <summary>
/// Get or Set the is validate.
/// </summary>
public Boolean IsValidate { get; set; }
/// <summary>
/// Implementation of phone number validation.
/// </summary>
/// <param name="value"></param>
/// <param name="cultureInfo"></param>
/// <returns></returns>
/// <Developer>Kaushik Kumar Mistry</Developer>
/// <DateCreated>25 May 2013</DateCreated>
/// <ModifiedBy>...</ModifiedBy>
/// <ModifiedDate>...</ModifiedDate>
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
ValidationResult result = new ValidationResult(true, null);
if (this.IsValidate)
{
String strContactNumber = value as String;
if (String.IsNullOrEmpty(strContactNumber))
result = new ValidationResult(true, null);
else if (Regex.IsMatch(strContactNumber, @"^\d{3}-?\d{3}-?\d{4}$") == false)
result = new ValidationResult(false, "Invalid Phone number.");
}
return result;
}
}
}
|
No comments:
Post a Comment