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 BASIC or 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. Start the Designer.
  2. In the Schema Repository Explorer view, expand the Record Types folder in your schema and then double-click on a record type.
    The record type editor opens.
  3. Click the Fields tab to view the Record Fields grid.
  4. 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 > BASIC or SCRIPTS > PERL.
    If Instant Editing Mode is enabled, the script editor opens. 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.

  5. Enter the code to validate user input after these lines (in Perl, the lines begin with #):
    
    REM Return a non-empty string explaining why the
    REM field's current value is not permitted.
    REM Or, if it is valid, return an empty string value.
    REM Example: 
    REM Dim value_info
    REM Set value_info = GetFieldValue(fieldname)
    REM If Len(value_info.GetValue()) < 10 Then
    REM resolution_date_Validation = "Must be at least 10 chars long"
    REM End If
    
    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:
    
    REM Return a non-empty string explaining why the field's current 
    value is not permitted
    REM Or, if it is valid, return an empty string value.
    
    value = GetFieldValue(fieldname).Get Value()
    if Not IsNumeric(value)
      user_number_Validation="Field does not contain a number."
    Else If (value < 1) or (value > 100) then
      user_number_Validation="User number must be between 1 and 100."
    end if