Validating input

Your voice application will automatically do some validation of the caller's input using properties that you set in your attributes classes. For example, the maximumKeys property of the DTMFAttributes class will ensure that the caller enters the correct number of keys. To perform more complicate validation you can create your own validation class. This class must implement the InputValidator interface, which has one method, validate(). This method takes as a parameter the InputResult object returned by the Call.playAndGetInput() method used to get the caller's input. The validate() method returns the validated input in the form of a String, or null if the input is not valid. Your implementation of this method is where the validation takes place.

Once you have created a new validation class you need to create an instance of it in your application. You then set this instance as the validator property of your InputAttributes object, using InputAttributes.setValidator(), or the InputAttributes constructor method. When you use this InputAttributes object as a parameter of the Call.playAndGetInput() method, your implemented validate() method is called to perform its validation. For example:

Creating the validator class
// Create an InputValidator class to validate the caller's telephone number
public class PhoneNumChecker implements InputValidator {
  public String validate(InputResult result) {
    // Get the caller's input from the InputResult object
    String value = result.getValue();
		
    // Process the input data
    // If the caller's number starts with a 2, it is valid
    if (value.substring(0, 1).equals("2")) return value;
		
    // Otherwise the number is invalid, so return null
    return null;	
  }//validate
}

Using the validator class within your application

public class myVoiceApp extends WVRApplication {
  .
  .
  .
  // Create an instance of the new validation class
  private PhoneNumChecker phoneNumChecker = new PhoneNumChecker();
  // Set the validator in InputAttributes
  telNoInputAtts.setValidator(PhoneNumChecker);
  // Use the InputAttributes object with the playAndGetInput() method.
  // The caller's response will automatically be checked to see if it begins
  // with a 2
  call.playAndGetInput(telNoPlayAtts, telNoInputAtts, telNoDTMFAtts, null);
  .
  .
  .
}