Thursday, April 6, 2017

Auto format number items in APEX

One of the questions that I get constantly is how to make a number format masks live - to format number fields as you type or when you leave an item.

In APEX you can define a number format masks in property Format Mask of number fields:


But it's only affected on page load and after page submit for validating format. There's nothing that prevents end user to enter a string or a number in wrong format.




There's a great JS library for that - autoNumeric.js. So let's see how can you implement it in your apps.

1) Download autoNumeric.js from GitHub (this demo works with autoNumeric library version v2.0.13).

2) Unzip the file and upload autoNumeric.min.js (from dist folder) to your workspace (or application) files (Shared Components > Static Workspace/Application Files) or to your server file location.


3) Add reference to the file in your application. I usually do it under the Shared Components > User Interface Attributes > Desktop > JavaScript File URLs


Now you can use it in your APEX apps. To use it for all APEX number type items you can create onLoad dynamic action on the global page (in most apps this is page 0). Number type items have CSS class number_field so you can initialize AutoNumeric on it, for example:


To explicitly override defaults from the global page you can create onLoad dynamic action on the specific page (remember to put greater sequence id from the one on the page 0) and use the update method to change the default properties:


For other methods and properties see autoNumeric.js documentation.

You can see demo here.

Edit (Thanks to Jorge Rimblas): You don't have to hardcode group and decimal separators, you can get them with apex.locale.getGroupSeparator() and apex.locale.getDecimalSeparator().

Enjoy!

Tested on APEX 5.1.1.00.08




2 comments:

  1. hi, i can't see the images and can't set the things to work, can you please re-upload the images? and also please inform how to set the text item (Number field) for: with decimals, integers only and other formatting.

    ReplyDelete
  2. hi, please ignore my previous comment, i have done, please check my topic for it on oracle community forum: https://community.oracle.com/thread/4277443
    as i am beginner, want to ask about how you arrange your input items on this demo: https://apex.oracle.com/pls/apex/f?p=100309:29 will you please guide/help to achieve it or a link to the guide? I will be much grateful, i can just use property option Start New Row to No but your's are attractive to me.
    regards

    ReplyDelete