The following event handler is assigned as the valid event handler for an order number entryfield in a form for entering new orders. The order numbers are preprinted on a paper form. The validation checks if the order number is valid, and if it’s valid, whether the order has already been entered.

function ORDER_NUM_valid

local lRet 

   lRet = false 

   if form.nextObj == form.cancelButton 

      lRet := true // Allow cancel 

   elseif empty( this.value ) or ; 

      transform( val( this.value ), "@L " + this.picture ) # this.value 

      this.validErrorMsg := "Order number must be four digits" 

   elseif keymatch( this.value, tagno( "ORDER_NUM" )) 

      this.validErrorMsg := "That order has already been entered" 

   else 

      lRet := true 

   endif 

return lRet 

The order number field is the first field in the form that gets focus, so the user must get by this validation to continue. If they click the Cancel button—on this form, it’s appropriately named cancelButton—the validation is bypassed so that the user can cancel the new order. The format of the field value is checked to make sure it is not blank and the correct length. A picture is set in the control to enforce digits-only, but it can’t check for length; the valid routine does. Finally, KEYMATCH( ) checks if that order number has already been entered. If it passes these checks, the valid returns true. If either check fails, the validErrorMsg is set appropriately and the event handler returns false so that the message is displayed and the focus remains in the control.