Question

I need to restrict a text box to 2 decimals

Hi,

I need to restrict a text box to 2 decimals (For ex; User should not be able to enter after 2.12)

Thanks,

Ravi

***Moderator Edit-Vidyaranjan: Updated Platform Capability***

Comments

Keep up to date on this post and subscribe to comments

November 28, 2019 - 4:46am

Any Solution?

Pega
November 28, 2019 - 5:06am

Please try the below workaround:
1. Open the "UserWorkForm" and add the below script.

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

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

November 28, 2019 - 5:14am
Response to Rakshith

I tried as you said, But it's not working still allowing after 2 decimal values.

November 28, 2019 - 5:30am

Hi

Can you try with an Edit-Input rule with below code:

---------------------------------------------------------------------

String input1,input2;
if(theValue.contains(".")){
input1 = theValue.substring(theValue.indexOf("."),theValue.length());
if(input1.length()>2){
theValue = theValue.substring(0,theValue.indexOf(".")+3);
}
if(input1.length()==2){
theValue = theValue+"0";
}
if(input1.length()==0){
theValue = theValue+"00";
}
}
else{
if(theValue.length()!=0){
theValue = theValue+".00";
}

}

 

----------------------------------------------------------------

Regards

Bhavya

November 28, 2019 - 5:49am

I tried with Edit-Input also but not working.

I referred Edit-Input rule in advance tab of property.

I am using 7.2.2 Version.

November 28, 2019 - 8:38am
Response to RaviY230

Try to use this edit validate:

  1. if (theValue.contains(".")) {
  2. String[] str = theValue.split("//.");
  3. theValue = str[0] + "." + str[1].substring(0, 2);
  4. }

And on the control, add an event "On Change" with action "Post Value"

November 28, 2019 - 10:52pm
Response to vaspoz

Hi Vaspoz,

it's not working still allowing more than 2 decimal points.

 

November 29, 2019 - 3:55am
Response to RaviY230

Sorry, not exactly correct solution =\

here's the right one (just tested on 7.3.1):

  1. if (theValue.contains(".")) {
  2. String[] str = theValue.split("\\.");
  3. if (str.length == 2) {
  4. int size = str[1].length() > 2 ? 2 : str[1].length();
  5. theValue = str[0] + "." + str[1].substring(0, size);
  6. }
  7. }

Plus, the action should be not "Change" but "Any Key"

Pega
November 29, 2019 - 12:08am

Hi,

In the text input cell properties, on the Presentation tab, you can set the read only format as number and can set the number of decimal places.

Is there any business requirement that you should use only Text input?

If not you can directly use Decimal control in which you can restrict the number of decimal values on parameters tab.

Thanks,

November 29, 2019 - 3:55am
Response to mutha1

Hi Mutha,

I am using decimal control only now, but my requirement is user should not enter more than 2 decimal points.

Ex:- 12.99876 User should not enter after 12.99

Pega
November 30, 2019 - 12:02am

Hi Ravi,

Thanks for posting the query. Can you try the below javascirpt code check it if is restricting 2 decimals

<script type="text/javascript">
  if(document.getElementById("Prop") !=null){
            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>