Question

Restrict an user to certain decimal points

Hi,

I need to restrict a decimal property to 2 decimals (For ex; User should not be able to enter after 2.12). I tried using pyDecimalPrecision in property but that is not working as expected. It is rouding off to highest number.

Can any one know the exact solution on how to prevent user after entering two numbers after decimal.

Correct Answer
April 15, 2019 - 1:45am

Thankyou everyone for your comments. They are very helpful.

I had written an edit input rule and solve my requirement.

Comments

Keep up to date on this post and subscribe to comments

March 20, 2019 - 10:00am

You can use integer,number or currency control and give decimal places 2 in read only format in presentation tab and enable checkbox "Display value using read-only formatting" .Using this user can enter any number like 23.2345 but once user leave focus it will get converted to 23.23 automatically.

March 23, 2019 - 12:18am

Hi,

Thank you for posting your question.

The functionality of "pyDecimalPrecision" Qualifier is to round of to the mentioned places.

This is achieved by passing the precision parameter to the "Decimal" control as below.

It is not designed to restrict to the user from entering after mentioned value.

Kindly follow the below steps to achieve your requirement.

1. Open the "UserWorkForm" and add the below script.

<script>
$('#DecimalProp2').on('input', function () {
        this.value = this.value.match(/^\d+\.?\d{0,2}/);
    });
</script>

2. In the place of "DecimalProp2" you should add the element id of your property. You can find that using browser debugger tool as below.

 

Thanks,

March 25, 2019 - 2:42am
Response to Priyabharathi

Hi,

I have tested the javascript code it work like if i entered 12.4566 it returns 12.45

but my requirement is it does not allow to enter thrid number after decimal.

ex:12.45 after 5 if enter some thing it does not take input.

March 28, 2019 - 3:25am
Response to RaghavenderReddyL

Hi,

The above code I provided will not allow user to enter after two decimal values.

In the UserWorkForm replace the DecimalProp2 with your property ID.

This is working fine at my end.

Thanks,

September 13, 2019 - 1:14am
Response to Priyabharathi

  Hi Bharathi,

  Could you please Provide Some screen shots?

 1. Where we can open the "User Work Form"?

 2. Where we have to write this Script Which you provide?

Thanks,

 

September 13, 2019 - 1:03pm
Response to R. Parusharam

Hi Parusharam,

UserWorkForm is a HTML Fragment rule belonging to Pega-UIEngine ruleset.

You can add the script in that rule.

Regards,

March 25, 2019 - 7:09am

Try this. 

<script type = "text/javascript" >
    document.getElementById("Prop").onkeypress = function(event) {
        var charCode = document.getElementById("Prop").value.toString();
        if (charCode.includes(".")) {
            var numb = charCode.split(".")[1];
            if (numb != null && numb.length > 1) {
                alert("Only 2 decimal places allowed");
                return false;
            }
        }
    }; 
</script>

I tried this at my end and it seems to be working.

March 25, 2019 - 11:40pm

Also, add a Null check before this, so that we do not get the console error messages in Developer tools for the screen which does not have this property.

Please find the attached code.

April 15, 2019 - 1:45am

Thankyou everyone for your comments. They are very helpful.

I had written an edit input rule and solve my requirement.

September 12, 2019 - 2:52pm
Response to RaghavenderReddyL

   Hi Raghavendra,

       I  got the Same requirement, Could you please provide your Edit Input code at here what you have written. That would be a great help.

Thanks in Advance.

September 13, 2019 - 12:40am
Response to R. Parusharam

Hi parusharam,

please find the edit input rule. it is used to modify the the value entered by the user.

 

September 17, 2019 - 7:57am
Response to RaghavenderReddyL

Thank you,

It's Working Fine.

i have created Edit Input rule by which you given the code.