Saturday, October 23, 2010

Range Validation Rule

In this post I will show you one of the rule which is quite often used in applications is the Range validation, To check the range of the input user control so that user can't enter less or more then the minimum and maximum value respectively. In my previous post I have show you how to use the validation rules you can read it if you don't have any idea about the validation rules.This post is the continuity of the previous post.
Let us start with the range validation rule example. The example code for this example is the same as I have used for the previous post I have added new validation rule with the name of the RangeValidation in the ValidationRules folder, where all the validation rules are placed.In the List 1 you can see the complete class for the RangeValidation rule. Here you can see that I have three properties one for the MaximumLength  and MinimumLength for checking the maximum values entered in the user input control and to check the minimum value entered by the user in the input control respectively.

public class RangeValidation : ValidationRule { public int MaximumLength { get; set; } public int MinimumLength { get; set; } public string FieldName { get; set; } public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo) { string strValue= value as string; int intLength = (strValue == null) ? 0 : strValue.Length; if ((intLength > MaximumLength) && (MaximumLength > 0)) return new ValidationResult(false, string.Format("{1} should be no longer than {0} characters.", MaximumLength, FieldName)); if (intLength < MinimumLength) return new ValidationResult(false, string.Format("{1} should be no shorter than {0} characters.", MinimumLength, FieldName)); else return new ValidationResult(true, null); } }
List 1

In the Validate override function you can see that I have first taken the value of the which is passed in the value parameter and then check the length of the string entered by the user by using the conditional operator. Then check for the maximum value if the MaximumLength is greater then 0 as the default value for the int property is 0 if you didn't set the value of the MaximumLength. In the next if condition I will check for the minimum value and if the value is valid then it will return the validation result with true and null parameter mean user input is valid.
In List 2 I have the xaml which is the view end of the example, Here you can see that I have used the range validation only for the first name of the user input control. Here you can see that I have set the maximum length and the minimum length for the user input control and also the name of the field "First name" which is used in the error message.

< TextBox Grid.Row="5" Grid.Column="1" Margin="2,1,20,1" Height="23"> < TextBox.Text> < Binding Path="FirstName" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay" ValidatesOnDataErrors="True" ValidatesOnExceptions="True"> < Binding.ValidationRules> < validationRules:RequiredFieldValidation/> < validationRules:RangeValidation MaximumLength="50" MinimumLength="4" FieldName="First Name"/> < /Binding.ValidationRules> < /Binding> < /TextBox.Text> < /TextBox>
List 2
You can see the out of both the minimum and the maximum check in the Image 1 and Image 2 you can also see that field name is also used in the messages. Which is the First name in this case.
Image 1

Image 2

By using the properties in the validation rule you can pass additional parameter to the validation rules. As the Validate function only two parameter one is the value which is of type object and second one is the culture information but if you used the converter they (converter) can take parameter which is of type object as well. But the Validate function only take two parameter so in order to pass information to the Validate function you can use properties of the class to pass the information to the validate function.Hope you get some idea of how to pass extra information to the validate function.You can download the source code from here.  

All and any comments / bugs / suggestions are welcomed!

1 comment:

Abdellah BOUGHRARA said...

Hello Asim,
Can you please use javascript library, that let us view your snippets properly. Personnaly i use it here: