Validation
Compose Enterprise is going through a design update. The illustrations on this page might be of the old design, but the information is up to date. Thank you for your patience during this update!
The utility of data gathering is defined by the quality of the gathered data. In order to make sure that end-users provide the correct answers in workflows, you can use validations (data types).
Validations are a set of rules defining the format of a question answer from the end-user. If the answer doesn’t comply with the validation rules, then the end-user is given a message and is prohibited from submitting the workflow. Compose has built-in validations like String, Integer, Decimal and Date and you may create custom validations in the validation builder. Some frequently used custom validations are email, SSN, Employee ID etc. In this user guide, we will create an email validation through a script and a generic service.
Choose the workspace and folder in which you would like to save the validation by clicking on the folder icons. Click the New Validation button in the top menu.
A dialog box appears. You can choose to create a validation for files or for text. For this example we are creating a validation to verify the text entered by the user, and we therefore select Text. Enter a name for our validation and click OK.
The validation appears on the screen. Double-click it to open it in the validation builder.
A validation can be defined by two methods; by a script or by a response from a generic service. We will start out by scripting a validation.
Validation by Groovy script
Within the validation builder there are three sections, Edit, Published and Languages. To start editing your validation, select the Edit section. If the validation has not been published before, all validation properties will be editable. For this example we will keep the default settings of the validation as a String with the input size minimum 1 and the default input size limit as 200.
Click on Script tab. Paste or enter a script in the Validation Code window, then click Save Draft. (Scroll to the bottom to view examples of scripts used for validation.)
If you wish to test which values will pass the validation test, click Test and a dialog box opens. In the dialog box enter an email address and click Test.
You can see if the email address passed or failed the validation code you have added. Click Close to exit.
In case the end-user enters an answer which doesn’t pass the validation, a Validation Error Message will be displayed to the end-user. You may customize this message in the input field below.
It is also possible to provide the validation in multiple languages (mainly the validation error message) by adding languages in the Languages section. The methodology of languages is demonstrated in the Languages in a form user guide.
If you’re happy with your settings then save the draft and click Publish. Click OK to confirm the publishing and the validation is published and ready to use in all forms in your workspace.
The published validation can be viewed in the Published section along with a list of items using this validation. If you are editing the draft of a published validation and make changes you regret, you can press the Revert to Publish button. The draft properties will be reset to match the published version of the validation.
Validation by a generic service call
A generic service can be used not only to define a format, but also to check whether the validated input is contained within a register. Say an end-user is creating a username, a generic service can check whether the username is available or not.
To use a generic service in a validation you need a previously created and versioned generic service (see the Integrations and pre-population folder of user guides to get started creating a generic service). In the Edit section, select the Generic Service tab and click Browse to choose the generic service you wish to use for the validation.
The generic service must be in the same workspace. Select the generic service you wish to use and click OK.
You may change the version of the generic service if it has been versioned multiple times. If you want the validation to be automatically updated any time a minor change is done to the generic service, check the Update to highest minor version automatically check box. In this way you would only have to update the validation if major changes are done to the generic service.
Next enter the map request variables menu and map the request variable of the generic service to the input value (as it is the input value from the end-user which will be validated).
After the request variable has been mapped, select the response variable which will indicate the passing/failing of the validation in the map response variables menu.
If the response from the service is 0 or false it is considered a failure. All other values are considered a successful validation. (If any other values should be interpreted as a failure, you will have to enter a transformation script for the response variable in the generic service builder.) Before publishing the validation you may test it in the same way as you did for the scripted validation above.
Customize the validation error message and languages as specified for the first example above. Save the draft and publish the validation by clicking the Publish button.
The validation can now be used by all forms within the workspace.
Use a validation in a form
To use a validation you need to create a form or open an already existing form. If you need more information on creating a form, please refer to the Build a simple form user guide. Select the question you want to control with the created validation. Under Input in the Properties panel you can select the validation you have created in the dropdown list.
Save the form draft and preview it to test the validation in the respondent UI. Underneath are some example validation scripts you can add to your own Compose installation:
Validation Scripts
Norwegian SSN check
if (!value.matches("[0-9]{11}")) {
return false;
}
int d1 = value[0].toInteger()
int d2 = value[1].toInteger()
int m1 = value[2].toInteger()
int m2 = value[3].toInteger()
int y1 = value[4].toInteger()
int y2 = value[5].toInteger()
int i1 = value[6].toInteger()
int i2 = value[7].toInteger()
int i3 = value[8].toInteger()
int c1 = value[9].toInteger()
int c2 = value[10].toInteger()
// Validate checksum c1
int c1calc = (((3 * d1) + (7 * d2) + (6 * m1) + m2 + (8 * y1) + (9 * y2) + (4 * i1) + (5 * i2) + (2 * i3)) % 11);
if (c1calc > 0) {
c1calc = 11 - c1calc;
}
if (c1 != c1calc) {
return false;
}
// Validate checksum c2
int c2calc = (((5 * d1) + (4 * d2) + (3 * m1) + (2 * m2) + (7 * y1) + (6 * y2) + (5 * i1) + (4 * i2) + (3 * i3) + (2 * c1)) % 11);
if (c2calc > 0) {
c2calc = 11 - c2calc;
}
if (c2 != c2calc) {
return false;
}
return true;Email validation
if (!value.matches("[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@([A-Za-z0-9-])+(\\.[A-Za-z0-9-]+)*\\.[a-z]{2,6}")) {
return false;
} else if (value.length() > 254) {
return false;
} else if (value.indexOf("@") > 64) {
return false;
} else if (value.endsWith("test")) { // The other reserved TLDs (invalid, example and localhost) are handled via the 6 character limit.
return false;
}
return true;Note that this might let through a few false positives since the top level domains (TLDs) are just checked to be between 2 to 6 characters. Also this does not support internationalized country code top-level domains.
Norwegian bank account number
Norwegian bank account numbers are 11 characters long and the last digit is a checksum of all the previous digits. Here is a validation script for account numbers:
if (!value.matches("[0-9]{11}")) {
return false;
}
int checksum = value[10].toInteger();
int sum = 0;
int[] calculationWeights = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2];
for (int i = 0; i < 10; i++) {
int part = value[i].toInteger();
sum += part * calculationWeights[i];
}
int calculatedChecksum = sum % 11;
if (calculatedChecksum != 0) {
calculatedChecksum = 11 - calculatedChecksum;
}
return checksum == calculatedChecksum;Here are a few examples of what account numbers can look like:
63450603017
63450618340
63450609198
63450602142
63450711464
Norwegian organizational number
The following validation script could be used for validating Norwegian organizational numbers.
def result = false
if (value != null) {
def valueAsString = value.replaceAll(" ", "")
if (valueAsString.length() == 9) {
def weights = [3, 2, 7, 6, 5, 4, 3, 2]
def total = 0
for (def i = 0; i < weights.size(); i++) {
total += (valueAsString[i].toInteger() * weights[i])
}
def remainder = (total % 11)
if (remainder != 1) {
def controlNumber = (remainder == 0 ? 0 : (11 - remainder))
result = (controlNumber == valueAsString[8].toInteger())
}
}
}
return resultIt can validate organizational numbers in the below format (note two spaces present between groups):
123 456 785
913 459 393
913 459 504
If it's necessary to validate the organizational number without allowing any white spaces between numbers, the script below could be used:
def result = false
if (value != null) {
def valueAsString = value.toString()
if (valueAsString.length() == 9) {
def weights = [3, 2, 7, 6, 5, 4, 3, 2]
def total = 0
for (def i = 0; i < weights.size(); i++) {
total += (valueAsString[i].toInteger() * weights[i])
}
def remainder = (total % 11)
if (remainder != 1) {
def controlNumber = (remainder == 0 ? 0 : (11 - remainder))
result = (controlNumber == valueAsString[8].toInteger())
}
}
}
return resultNumeric number (0-9)
Without spaces
value.matches("[0-9]*")With spaces
value.matches("^[0-9\\s]*[0-9]+[\\s]*\$")