如何在Oracle APEX中设置数字、社保号及手机号的格式掩码?
Great question! Let's break this down step by step, since APEX's format mask behavior can be a bit context-dependent depending on the item type you're working with.
1. Setting Format Masks for All Numeric Values
First off, you're right that some APEX item types (like Date Pickers or Currency Fields) restrict the Format Mask to their specific data types, but numeric format masks are fully supported for Number Field items. Here's how to use them:
- Navigate to your page item's properties, set the Type to
Number Field. - In the Format Mask field, use Oracle's standard numeric format models. Some common examples:
- Integer with no decimals:
999999(renders as123456) - Number with 2 decimal places:
999999.99(renders as123456.78) - Number with thousands separators:
999G999D99(uses your locale's default separators, e.g.,123,456.78) - Percentage format:
999D99%(converts0.75to75.00%)
- Integer with no decimals:
Just ensure the format mask aligns with the data type of your underlying column (e.g., don't use a decimal mask for an integer-only column).
2. Formatting SSN (999-99-9999) and Phone Numbers (999-999-9999)
APEX's native "Format Mask" for numeric items doesn't handle fixed separators like hyphens in SSNs/phone numbers directly—this is where you have two solid options, depending on whether you need formatting during user input or for display-only purposes:
Option 1: Input Mask for Real-Time User Entry
If you want users to type digits and have hyphens auto-insert as they go:
- Use a Text Field item type (instead of Number Field, since we're working with formatted text).
- In the item's properties, find the Input Mask field (under the "Element" section) and enter:
- For SSN:
999-99-9999 - For Phone Number:
999-999-9999
- For SSN:
This enforces the format as the user types. If you need to store the value without hyphens, add a validation or process to strip them using REPLACE(:P1_PHONE, '-', '') or REPLACE(:P1_SSN, '-', '').
Option 2: PL/SQL for Display-Only Formatting
If you're formatting values stored in the database (e.g., in a report or read-only page item), use Oracle's TO_CHAR function with a custom format string:
- For SSN (stored as a number):
TheTO_CHAR(ssn_column, 'FM000"-"00"-"0000')FMremoves leading spaces, and quoted hyphens insert the required separators. - For Phone Number (stored as a number):
TO_CHAR(phone_column, 'FM000"-"000"-"0000') - If the value is stored as a hyphen-free string, use
REGEXP_REPLACEto add separators:REGEXP_REPLACE(ssn_string, '(\d{3})(\d{2})(\d{4})', '\1-\2-\3')REGEXP_REPLACE(phone_string, '(\d{3})(\d{3})(\d{4})', '\1-\2-\3')
You can use these expressions directly in a report column's Format Mask (by selecting "PL/SQL Expression" as the format type) or in a page item's Source as a PL/SQL expression.
内容的提问来源于stack exchange,提问作者Jordan Holmer

