Validators: Concluding Remarks - Creating Custom Validators
(Page 2 of 4 )
Though the Validator framework provides most of the rules, there will be times when a custom rule (which even validwhen wouldn’t satisfy) must be applied. In such circumstances, there are two possible approaches. The first one involves writing JavaScript to apply the rule. But in this case the same trouble raises its head -- the problem of reuse.
The other approach follows the path of the Validator framework. This ensures reuse of the rule without embedding the JavaScript explicitly by the web designer. As an example I will be using the registration page itself. There is a field for phone number entry. The required rule is that the entered number has to follow a certain pattern. Though this is possible through the mask Validator, I decided to go ahead with creating a custom rule, as it would also help in experimenting with the Validator framework. So let's see how it is done, without much ado.
To create a custom Validator, certain steps must be followed.
First, you need to create a Java class that will contain the validation logic. This is the basis of it all. The class would contain only one method. The method must be static, so that the framework can call it without having to create an object.
The class I have created is as follows:
public class PhoneRules
{
:
:
}
Next: Define the validation logic >>
More JavaScript Articles
More By A.P.Rajshekhar