Validating user input in fields

You can use a validation hook to verify that the information a user has entered in a field is valid. If the information is not valid, the user is prompted for valid information.

About this task

When you define a new Perl hook for a field or action, the calling syntax for that hook is added to the script editor window. The calling syntax cannot be edited.

Because hooks can affect the behavior of a field, design and test hooks carefully before making them available to users. For example, this user input hook example effectively makes the user_number field a mandatory field, regardless of the setting in the Behaviors grid.

Procedure

  1. In the Workspace, expand Record Types and the record type; then double-click Fields.
  2. In the Fields grid, click the Validation cell of the field you want to modify and click the down arrow to display a list of available hooks. Click SCRIPTS > PERL.
    If Instant Editing Mode is enabled, the Designer starts the script editor. If Instant Editing mode is disabled, double-click the Validation cell of the field to start the script editor.

    BASIC and Perl have their own script editors. The Designer indicates the type of editor in the title bar of the Designer window. Verify that you are in the correct editor before editing code.

  3. Enter the code to validate user input after these lines:
    # Return a non-empty string explaining why the
    # field's current value is not permitted.
    # Or, if it is valid, return an empty string value.
    # Example: 
    # my $value_info;
    # $value_info = GetFieldValue(fieldname);
    # If (length(value_info.GetValue()) < 10) {
    #     $resolution_date_Validation = "Must be at least 10 chars long"
    # }
    For example, if the field name is "user_number" and its type is INT, the code ensures that users enter a value between 1 and 100:
    # Return a non-empty string explaining why the field's current 
    value is not permitted
    # Or, if it is valid, return an empty string value.
    
    my $value = GetFieldValue($fieldname)->GetValue();
    my $user_number_Validation;
    
    if (! $value =~ /^-?\d+(\.\d+)?$/) {
        $user_number_Validation = "Field does not contain a number.";
    } elsif ($value < 1 || $value > 100) {
        $user_number_Validation = "User number must be between 1 and 100.";
    }
  4. Click Hooks > Compile. The script is compiled and any syntax errors are identified.
  5. Fix any errors and click Hooks > Compile or close the Script editor.