Displaying Error Messages using JavaScript API in Oracle Apex

In this tutorial, I will show you how you can validate an item on lost focus event and display the custom error messages using the JavaScript API apex.message.showErrors() method in Oracle Apex.

Display Error Messages using JavaScript in Oracle Apex

To display the custom error message for an item follow these steps:

Create the following JavaScript function in the "Function and Global variable declare" section of the page:

function showError(pData, pItem) {
    apex.item(pItem).setFocus();
    apex.message.clearErrors();
    apex.message.showErrors([{
            type: "error",
            location: "inline",
            pageItem: pItem,
            message: pData,
            unsafe: false
        },
        {
            type: "error",
            location: "inline",
            message: "An error has occurred.",
            unsafe: false
        }
    ]);
}

The above JavaScript function accepts two arguments, one is the error message text as pData and the another one is page item name as pItem.

Now suppose you want to validate the salary which should be greater than from 2000.

Then create a dynamic action on the salary field and select the event Lose Focus.

And create a TRUE action as Execute JavaScript code and add the following code in it:

if ($v("P3_SALARY") < 2000) {
    showError("Salary must be greater than 2000.", 'P3_SALARY');
} else {
    apex.message.clearErrors();
}

The above JavaScript code will check for the item P3_SALARY and if the entered value is less than 2000, it will show the inline error message.

Similarly, you can create the same dynamic action for other items and call the showError JavaScript function with the error message and the item name to display error messages.

If you want to validate value from the Oracle Database table then refer the following post: Oracle Apex item validation using JavaScript.

Vinish Kapoor
Vinish Kapoor

Vinish Kapoor is a seasoned software development professional and a fervent enthusiast of artificial intelligence (AI). His impressive career spans over 20 years, marked by a relentless pursuit of innovation and excellence in the field of information technology. As an Oracle ACE, Vinish has distinguished himself as a leading expert in Oracle technologies, a title awarded to individuals who have demonstrated their deep commitment, leadership, and expertise in the Oracle community.