ࡱ > 0 _( bjbjVV 4v < < ^
e e e y y y y D y . Z Z Z - - - - - - - $ / h2 P - e Z 8 " Z Z Z - - Z F 8 e - Z - + h M , 9 y , - - 0 . -, 2 @ ~ 2 , 2 e , Z Z Z Z Z Z Z - - Z Z Z . Z Z Z Z 2 Z Z Z Z Z Z Z Z Z
: Validators
Webforms are always collecting input from users. The problem is that Users always enter invalid data, intentionally or not. Hence there was the need to create a validation module to validate the posted values against the desired. These validators should also direct the end user to the way to insert the correct data.
Overview
JSPX is shipped with half dozen of Validators to validate user input. These validators are web controls that are declared on the JSPX page, and associated with controls that are accepting data from user like (Input control and Select) in order to validate their values.
Standard HTML does not provide a control to do so, Jspx is introducing a new Tag for such a functionality
Validation in other Frameworks
Looking into the existing frameworks like JSF, the validators are provided also. But they lacks user friendly features like, the fact the JSF validators are making validation only on the server side. Where this is make it quite easy for jsf to validate, but costing the webserver extra hits. When I looked around for a reason for that, the answer was Some Browsers Doesnt provide JavaScript. Id leave the comment for you, as you may wonder How would the LinkCommand will work then?. Something also, that is the developer finds a difficulty to customize the message displayed to the end user.
Looking into something outside java like ASP.NET, we find that the validation is done only on the client side. Despite the beauty of this as this saves number of invalid hits on your webserver, this also requires more developer logic to re-validate the input data.
Validation in jspx
Jspx is providing a validation that takes the best out of such two ways. Jspx validators validate the values both the server side and client side. Where the values are validated on the client side first and then posted to the server. On the server side the validators start to validate the posted data once again. This is done to prevent data attack from non-browsers clients.
The server side validation is done before the invocation of the server side events to make sure that the server side event handlers code runs on clean inputs.
Now lets have a look on the use of validators on a simple form. The validators are used as following.
In this example, we have a single text box that is accepting a serial number from the user. This serial should not be empty and numeric values only of length 19 digits. For that set of validators are used to achieve that.
A required field validator
Numeric validator
Length validator.
Whenever one of these validators is violated in server or client side, the error message will be displayed wherever the validator is. It is allowed to place the valdiator in any place in the page.
Types of validators
Lets have a look on the different types of validators.
TypeDescriptionRequiredValidates that a control should have a value, and cannot be empty.NumericValidates that a control should have a numeric value. Empty is allowed.LengthValidates that a control should have a value of specific character length, regardless the type of data(String or Number)RangeValidates that a control should have a value that lies between Max and Min Values EmailValidates that a control should have a value that is a valid email address.
Validator attributes
The type of validator is defined when one of the above types is assigned to the attribute type in the HTML Tag Validator. Now lets inspect the different attributes of this Tag.First we will see the common attributes among all validators.
NameTypeRequiredDescriptionidStringYesThe Id of this validator.typeenumYesThe Type of this validator one on the following
Required
Numeric
Length
Range
Email.control_to_validateStringYesThe id of the web control this validator is going to validate.GroupStringYesThe name of the validation group this validator is belonging to, when this value is missing, an exception will be thrown.MessageStringNoThe Error message that will be displayed in case of error only
e.g. username is required.Indicator StringNoAn indicator message to help the user to insert the correct value.
e.g. valid email address.
Extra attributes
In addition to the above attributes, some types of validators require extra attributes.
Range Validator
NameTypeDescriptionMinValNumberThe minimum value that the control to be validated should not be less than it.
If this filed is not specified then it will not be validated.MaxValNumberThe maximum value that the control to be validated should not exceed it.
If this filed is not specified then it will not be validated.
Length Validator
NameTypeDescriptionMinLengthNumberThe minimum length that the control to be validated should not have a value with length less than it.
If this filed is not specified then it will not be validated.MaxLengthNumberThe maximum length that the control to be validated should not have a value with length exceeds it.
If this filed is not specified then it will not be validated.
As a common use case- just like the one we have here- we need to combine the range, length and numeric in one validator.
The numeric validator provides such functionality through the following extra fields.
Numeric Validator
NameTypeDescriptionLengthNumberThe length that value of a control should be.
If this filed is not specified then it will not be validated.MinDigitsNumberThe minimum length that the control to be validated should not have a value with length less than it.
If this filed is not specified then it will not be validated.MaxDigitsNumberThe maximum length that the control to be validated should not have a value with length exceeds it.
If this filed is not specified then it will not be validated.
Validation invoker
Validation action is always fired when some controls take action. For example a button is submitting a form, and then the validation should be fired before doing so. Invokers can be a Button, ImageButton or a LinkCommand. A new attribute is added to these controls, which is group that is used to invoke certain set of validators under this group.
More information about validation group is below.
Validation group
Consider that your website has a front page that provides two dialogs, one for login if you have existing account, and the other is to signup in case you dont.
If the user chooses to login, and provides a username only without a password and the clicked on the login button. The system should tell him that the password is required.
And if the user chooses to signup and left one of the required information in the signup form empty, the system should tell him to complete all required information.
It obvious that neither of the required fields in the signup form should affect the sign in form, and vice versa. So validation controls should be grouped by its business case. Jspx validator exposes an attribute is name Group that is used to flag validators under a group.
So whenever a control fires this validation group, only validators under this group will be executed.
Validation UI
The output of each validator is small blue icon beside the control to validate. Hovering on this blue icon will display indicator message as following.
In case of validation error, the control caused the error will be highlighted in red, and a small error red icon is displayed. Hovering on this icon will display the error message as following.
10. Validation in action
Jspx validation is a very easy and powerful feature that can be used to make rich and sophisticated applications. More examples on validators are available on jspx website.
It is important to note that validation will not be fired unless the attribute group is set
S
<
I
`
a
X m W X ƸƦƆƦtƦ #h B*
CJ OJ QJ ^J aJ ph #h%r B*CJ OJ QJ ^J aJ ph h%r CJ OJ QJ ^J aJ #h%r B*CJ OJ QJ ^J aJ ph? h CJ OJ QJ ^J aJ #h%r B*
CJ OJ QJ ^J aJ ph h h<, h