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.