Top
Performing Form field validation using a script
  • 1 minute to read
  • Print
  • Dark
    Light

Performing Form field validation using a script

  • Print
  • Dark
    Light

Form field validation is a Smart Tip feature that allows you to ensure that the information entered in a field on a form is compliant to certain predetermined criteria. When the out-of-the-box validations are insufficient, you can use advanced customization rules to define your own custom validations. Whatfix ensures that user input is validated against specific criteria and if the input is not found compliant, then an error message is displayed.

validation_error.png

Here is an example of a script that checks if the user input is a regular expression that is in a specific format or not. In the example below, the user input must be a three part entry.

  • The first part is a single alphabet from A to Z and must be followed by a space.
  • Then the second part can be a word that can contain any number of alphabets that are either small case or capital case.
  • The second and third part has to be separated by a hyphen.
  • And the last part can again be a word with any number of characters that are alphabets.
    If the input is not compliant, then an error message that says "Input Error" is displayed. If the entry is compliant, the validation is passed and the procedure is not interrupted.
Note
The flow ID for a Smart Tip collection can be seen when you navigate to a particular collection on your dashboard. It is the alpha-numeric part at the end of the URL.

Modify the snippet as per your validation requirement and include it in your integration script section after adding the flow_id and the desired tip number. In the example below, the flow id (Smart Tip Collection ID) is 18e651e0-169f-11ea-bbb3-04013d24cc02 and the tip (referred to as step in the code) to be validated is tip number 2.

Finding the ID of your Smart Tip collection is very similar to Finding the Flow ID of a Walkthrough.

function isRegExp(inText)
{
console.log("in isRegExp. inText: " + inText);
var retVal = false;
var regExp = new RegExp("^([A-Z]{1})\. ([a-zA-Z]+)-([a-zA-Z]+)$");
retVal = regExp.test(inText);
console.log("retVal = " + retVal);
return retVal;
}

function onValidate(event) {
console.log("in onvalidation");
var result = {
isValid: true,
error: "Input Error"
};
if (event.flow_id == "18e651e0-169f-11ea-bbb3-04013d24cc02" && event.step == 2) {
result.isValid = isRegExp(event.input);
return result;
} else {
return null;
}
}
Note
For additional configurations, see Smart Tip configuration.
Was this article helpful?