For this example I am using data element 'EBELN' from table EKKO.
Update: please note that in SAP release 6.10 and later you need to set the profile parameter "abap/fieldexit" to 1 (via tcode RZ10 or RZ11). System must then be restarted for any change to this parameter to take effect.
Execute program 'RSMODPRF', entering the data element of the field
Create function module when prompted, and add your code to the source section using the parameter 'INPUT' as the field value you are checking.
Save and activate Function and execute transaction 'CMOD'.
Choose option: Goto-> Text Enhancements->Data elements->New DE cust docu.
Enter data element
Save and activate
Execute program 'RSMODPRF' again but this time leave data element field blank.
Select the checkbox for the data element you have just created and choose option: Field exit->Activate. If you get the message 'System is not configured for field exits' then you need to set the system profile parameter "abap/fieldexit" to 1 and restart your SAP system.
This will now be active for all instances of this data element, but you can assign specific program and screen combinations by pressing the 'Assign prog./screen' button. You can also change the code you entered in the field exit function module by pressing the 'Edit FM' button.