Requirement: We have a Text Field, and we want to ensure that whatever value we enter in this Text Field is only a Number or a Decimal value (two-digit after the decimal point).
In case the user tries to enter any value other than the Number or Decimal value it will simply not be entered into the Text Field.
Also, if we enter more than two-digit after the decimal point says for example 12.567, it will simply round the value to 12.57.
Solution: For solution of the above requirement follow the steps as shown below:
Step 1: Create an Oracle APEX page. Create a Region inside the page and set Title as Allow Number and Decimal Value
Step 2: Create a Page Item inside the above created Region. Set the properties for the Page Item as below:
- Name: P6_ENTER_VALUE
- Type: Text Field
- Label: Enter Value
Go to the Advanced section of the property palette of the Page Item and set the below properties
- CSS Classes: allow-decimal
- Custom Attributes:
onfocusout="this.value=Number(this.value).toFixed(2)"
Here, the value 2 is to allow only two digits after the decimal point.
Step 3: Let us add the following jQuery code to the Execute when Page Loads section for the page:
$(".allow-decimal").keypress(function (e) { if(e.which == 46){ if($(this).val().indexOf('.') != -1) { return false; } } if (e.which != 8 && e.which != 0 && e.which != 46 && (e.which < 48 || e.which > 57)) { return false; } });
Save and run the application. Thus, the ran application is shown below:
Try to enter values other than Numbers or Decimal Point, and we can see it is not allowing us to enter the value.
Let us enter the value as 12.567 and we can see as soon as we move out of the field the value is rounded to 12.57.
Hence, the solution to our requirement.
If you like the post please comment, share, and do join me on Facebook. Please subscribe to my YouTube Channel for video tutorials.
Thanks & Regards,
Susanto Paul
506 total views, 1 views today