Sun. Oct 24th, 2021

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

By Susanto Paul

Susanto is an Oracle ACE, a qualified MCA, MBA, and a highly-skilled Senior Oracle Specialist. He is an enthusiastic Blogger and YouTuber who helps learners to solve their complex problems more efficiently. He has 9+ years of experience in multiple technologies like AWS, Oracle ADF, Oracle APEX, Oracle JET, Oracle VBCS, Oracle IDCS, Oracle PL/SQL, Oracle Integration Cloud, Java, JDBC, Servlets, JSP, Spring, Hibernate, HTML5, CSS3, JavaScript, TypeScript, NodesJS, Angular, MySQL, Oracle WebLogic Server, JUnit, JMeter, Selenium Web Driver, etc. He is a certified: Oracle Certified Professional Java SE 6 Programmer, Oracle ADF 11g Certified Implementation Specialist, Oracle Cloud Platform Application Integration 2020 Certified Specialist, Oracle Cloud Infrastructure Foundations 2020 Certified Associate, and Oracle Cloud Infrastructure Developer 2020 Certified Associate

Leave a Reply

Your email address will not be published. Required fields are marked *

satta king chart