Validator widgets provide an easy way to evaluate simple expressions and allow users to see different outcomes in a Mashup.
Using a validator is fairly intuitive for simple expressions, such as is my field blank? But if we need to evaluate a more complex scenario based on multiple parameters, then we can user our validator with a backing service that will perform more complex analytics.
To show how services can work in conjunction with a validator widget, let’s consider a slightly more complicated scenario such as: A web form needs to validate that the zip or postal code entered by the user is consistent with the country the user selected.
Let’s go through the steps of validating our form:
//Input parameters: Country and PostalCode (strings)
//Country-based regular expressions:
var reCAD = /^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$/;
var reUS = /^\d{5}(-\d{4})?$/;
var reUK = /^[A-Za-z]{1,2}[\d]{1,2}([A-Za-z])?\s?[\d][A-Za-z]{2}$/;
var search = "";
//Validate based on Country:
if (Country==="CAD")
search = reCAD.exec(PostalCode);
else if (Country==="US")
search = reUS.exec(PostalCode);
else if (Country==="UK")
search = reUK.exec(PostalCode);
(search == null) ? result = false: result = true;
Of course, in addition to providing a message we can also use the results of the validator to activate additional services (such as writing the results of the form to the database).
For an example you can import into your ThingWorx environment, please see the attached .zip file which contains a sample mashup and a test thing with a validator service.