User Image
Oracle APEX  getValidity()

Oracle APEX getValidity()

Hossam Assadallah

Hossam Assadallah

Creating DateAugust 10, 2024

Oracle apexJavascriptjQuery

The getValidity() method is part of the Oracle APEX JavaScript API and is used to determine the validity state of an APEX item, similar to how HTML5 form validation works. This method helps in checking whether the input for an item meets its validation constraints.

Understanding getValidity()

  • getValidity(): This method returns an object similar to the HTML5 ValidityState object. This object contains several properties that provide information about the validity of the item. These properties include:valid: A Boolean value that is true if the item is valid and false if any constraint is violated.valueMissing: true if the item is required and the user has not entered a value.typeMismatch: true if the item’s value is not in the expected format (e.g., an invalid email address).patternMismatch: true if the item’s value does not match the specified pattern.tooLong: true if the item’s value exceeds the maximum length.rangeUnderflow: true if the value is below the specified minimum.rangeOverflow: true if the value exceeds the specified maximum.stepMismatch: true if the value is not a valid step (used for number inputs with a step value).customError: true if a custom validation error has been set.
  • valid: A Boolean value that is true if the item is valid and false if any constraint is violated.
  • valueMissing: true if the item is required and the user has not entered a value.
  • typeMismatch: true if the item’s value is not in the expected format (e.g., an invalid email address).
  • patternMismatch: true if the item’s value does not match the specified pattern.
  • tooLong: true if the item’s value exceeds the maximum length.
  • rangeUnderflow: true if the value is below the specified minimum.
  • rangeOverflow: true if the value exceeds the specified maximum.
  • stepMismatch: true if the value is not a valid step (used for number inputs with a step value).
  • customError: true if a custom validation error has been set.

Example Usage

Here’s an example of how you might use getValidity() to check an item’s validity and respond accordingly

1var item = apex.item("P1_EMAIL");
2var validity = item.getValidity();
3
4if (!validity.valid) {
5    if (validity.valueMissing) {
6        apex.message.alert("Please enter a value.");
7    } else if (validity.typeMismatch) {
8        apex.message.alert("Please enter a valid email address.");
9    } else if (validity.patternMismatch) {
10        apex.message.alert("The input does not match the required pattern.");
11    }
12    // Add additional checks for other validity states as needed
13}
14

When to Use

  • Form Validation: To validate form inputs before submission, ensuring all required fields are correctly filled.
  • Real-time Feedback: To provide immediate feedback to users when they enter invalid data.
  • Custom Validation Logic: To implement custom validation logic and error handling based on specific validation states.

Considerations

  • Compatibility: Ensure that your version of Oracle APEX supports the getValidity() method.
  • Validation Constraints: Define appropriate validation rules for your items in the APEX application builder to leverage this method effectively.

Conclusion

The getValidity() method provides a powerful way to handle form validation in Oracle APEX applications, enabling developers to create responsive, user-friendly interfaces with real-time feedback on data entry.

May you like Heart Icon

No posts found in the category "Javascript"

Comments