Skip to main content
Kinetic Community

Creating Conditional Answer Validation

There can be a need for two different validations on a field, based on other answers in the service item. In this case you cannot use the built-in validation system and must handle the validation separately.

Usage

You can build a small javascript to enforce the desired format. In this example, the user is creating a new employee. That employee can be either a contrator or a full-time employee. The login ID, in this case, is either e or c, then the two digit representation of the start year, the new employee's three initials, and a two digit number (the next available in the sequence). The reason the standard validation doesn't work for this scenario is that if the new employee is a full-time employee, the login ID must start with an 'e', but if the new employee is a contractor, the login ID must start with a 'c'. The standard validation cannot be conditional like this. Instead, you can use the same type of validation patterns to check, but within the correct conditional statements.

Example

The javascript can have as complicated conditionals as necessary. In this case, the conditionals are quite simple.

function validateLoginID(loginId) {
    //Pull in the variable upon which the validation string is dependent.
   var empType = KD.utils.Action.getQuestionValue("Employee Type");
   var match = "";
   //if the employee type is Employee, be sure the login starts with an
   //e, contains a two digit number (representing the start year), the
   //three initials of the person, and a two digit number (the next available)
   if (empType == "Employee") {
      match = loginId.match(/^e\d{2}[a-zA-Z]{3}\d{2}$/g);
   }
   //if the employee type is Contractor, be sure the login starts with an
   //c, contains a two digit number (representing the start year), the
   //three initials of the person, and a two digit number (the next available)
   if (empType == "Contractor") {
      match = loginId.match(/^c\d{2}[a-zA-Z]{3}\d{2}$/g);
   }
   //Error if a match wasn't found then clear the id entered
   if (match == "" || match == null) {
      alert("The login ID you entered is not valid, please re-enter the login Id");
      KD.utils.Action.setQuestionValue("Login ID", "");
   }
}

Then this is called on change on the Login ID question.

LoginValidationEvent.png