Add Client Side Validation

Client side validation provides instant feedback to the user and reduces the load on the server. It can be used to designate fields as required, to enforce data types such as int, float and dates, and to specify a regular expression. The are two ways to specify validation properties - by implementing one of Validatable interfaces and by associating ValidationSpecs with the component. AjaxSwing adds Javascript listeners, which get activated when the focus is lost from the HTML input and if the entered data is invalid, AjaxSwing will displays popup window, which shows error message. Implementing com.creamtec.ajaxswing.support.validation.Validatable interface requires extending standard Swing component, whereas instantiating one of com.creamtec.ajaxswing.support.validation.ValidationSpecs descendents allows dynamically adding validation parameters to any Swing component.

AjaxSwing Client Side Validation Types

AjaxSwing supports a lot of client side validations through this interface or any one of it’s descendent. For example you can check user’s input against specific mask, required, range, …. etc, also you can specify custom error message for user or use default error message.

Current version of AjaxSwing has the following validation interfaces :

Example of associating validation specifications with a component

        IntSpecs ageSpecs = new IntSpecs();
        ageSpecs.setDisplayName("Age");
        ageSpecs.setMinValue(5);
        ageSpecs.setMaxValue(90);
        ageSpecs.setErrorMessage("Minimum value is 5 and maximum value is 90");
        ComponentUtils.setValidationSpecs(txfAge, ageSpecs);

Example Implementation of Validtable Interface

In this example we will create a new class called JTextFieldValidatable which exetends javax.swing.JTextField and implement com.creamtec.ajaxswing.support.validation.Validatable Interface.

package com.creamtec.ajaxswing.ui;
import javax.swing.JTextField;
import com.creamtec.ajaxswing.support.validation.Validatable;

public class JTextFieldValidatable extends JTextField implements Validatable {
       
        private String displayName;
        private String errorMessage;
        private String mask = Validatable.DEFUALT_MASK;
        private boolean required = Validatable.DEFUALT_REQUIRED;
        private int maxLength = Validatable.DEFUALT_MAX_LENGTH;
        private int minLength = Validatable.DEFUALT_MIN_LENGTH;
        
        public String getMask() {
               return mask;
        }
 
        public void setMask(String mask) {
               this.mask = mask;
        }
 
        public int getMaxLength() {
               return maxLength;
        }
 
        public int getMinLength() {
               return minLength;
        }
 
        public boolean isRequired() {
               return required;
        }
        
        public String getDisplayName() {
               return displayName;
        }
 
        public void setDisplayName(String displayName) {
               this.displayName = displayName;
        }
        
        public String getErrorMessage() {
               return  errorMessage;
        }
 
        public void setMaxLength(int maxLength) {
               this.maxLength = maxLength;
        }
 
        public void setMinLength(int minLength) {
               this.minLength = minLength;
        }
 
        public void setRequired(boolean required) {
               this.required = required;
        }
 
        public void setErrorMessage(String errorMessage) {
               this.errorMessage = errorMessage;
        }
       
}

In our implementation of JTextFieldValidatable we initialize all validations with default values, AjaxSwing will add validation to the component at rendering time only if its value is not equal to the corresponding default value. By this way you can disable specific validation by setting its value to the default.

In our code when we create instance of JTextFieldValidatable class, we will add our validation to the created instance :

        JTextFieldValidatable txfEmail = new JTextFieldValidatable();

Add required validation :

        txfEmail.setRequired(true);

Add email validation using predefined mask, AjaxSwing has a lot of predefined masks ( email, telephone, credit card …etc)

        txfEmail.setMask(Validatable.MASK_EMAIL);

If you want to use specific mask you can pass this mask to setMask method like the above example.

Add Custom Error Message :

        txfEmail.setErrorMessage(“User Email is required and must be valid mail”);

If you want to use AjaxSwing default error message instead of using custom error message, then you have to set Display Name, which will display in the default error message :

        txfEmail.setDisplayName(“User Email”);