Question

Function

I have the below function working but it does not allow negative numbers.

How can we make this adjustment....

Function Code:

java.util.regex.Pattern p = java.util.regex.Pattern.compile("^[0-9]\d*(\.\d+)?$");
java.util.regex.Matcher m = p.matcher(input);
return m.matches();

Correct Answer
November 21, 2018 - 3:24pm

I tried this and it appears to work great!

try {
            new BigDecimal(input);
        } catch (NumberFormatException e) {
            return false;
        }
        return true;

Comments

Keep up to date on this post and subscribe to comments

Pega
November 20, 2018 - 4:13pm

Hi , would it be possible to explain what you are trying to achieve using the function. Just thinking there might be some other solution.

Thanks,

Chithra.

November 20, 2018 - 5:10pm
Response to Chithra

I need to be able to identify an alpha character and flag 'custom' error message.

I am currently testing with a validate rule and/or constraint rule.

Validate For Non-Negative numbers

- Allow whole numbers and decimals.

- Flag 'custom' error message if user puts negative number.

-When property value <= 0

-This works fine.

Validate For Non-Alpha Characters

- Allow whole numbers and decimals.

- Flag 'custom' error message if user puts any Alpha Characters.

- Need to allow negative numbers for this one so that only the alpha message displays and not both messages.

- I checked all the OOTB Edit Validates and functions and could not find anything just for alpha. So I thought to create one.

Pega
November 20, 2018 - 5:44pm
Response to HenryA80

You could try this in a method since it considers "-" as optional and must have at least one digit:

return (input.matches("-?\\d+"));

 

 

 

November 20, 2018 - 6:00pm
Response to Chithra

When you say "method" could you explain...

Would I use this in a when rule, activity method, or validate rule.

November 20, 2018 - 6:02pm
Response to Chithra

or if you mean to add it to function can you show it all together  

November 20, 2018 - 6:11pm
Response to Chithra

Below is what I tried. It allowed the negative numbers which is good but not decimals.

java.util.regex.Pattern p = java.util.regex.Pattern.compile("^[0-9]\\d*(\\.\\d+)?$");
java.util.regex.Matcher m = p.matcher(input);
return (input.matches("-?\\d+"));

How to get decimals,whole numbers, negative numbers, but not alpha characters.

Pega
November 20, 2018 - 8:20pm
Response to HenryA80

I missed the inclusion of decimal in my previous response so here is the updated one:

return(input.matches("-?\\d+\\.?\\d+"));

This could be used in validate rule (function code) same way as you have done with "Validate For Non-Negative numbers".

Hope this helps!

November 20, 2018 - 10:26pm
Response to Chithra

-       Sorry to bother you again but here are my results.

-       We are almost there.

-       Ideally one function but maybe it needs to be more than one function. Not sure

Requirement

-       Allows decimal

-       Allows whole numbers

-       Allows Negative

-       Fails alpha

**Desire opposite result

 

return m.matches();

-       Allows decimal

-       Allows whole numbers

-       **Fails negative numbers

-       Fails alpha which is good

 

return (input.matches("-?\\d+"));

-       **Fails decimals

-       Allows whole numbers

-       Allows Negative

-       Fails alpha which is good

 

return(input.matches("-?\\d+\\.?\\d+"));

-       Allows decimal

-       **Fails whole numbers

-       **Fails negative numbers

-       Fails alpha which is good

 

 

 

Pega
November 20, 2018 - 10:59pm
Response to HenryA80

No problem! I would like to confirm if you are seeing the same result as I am with the last code snippet I shared with you:

code -

String input = "-19a0";
oLog.error(input+" result : "+input.matches("-?\\d+\\.?\\d+"));
input = "190.01";
oLog.error(input+" result : "+input.matches("-?\\d+\\.?\\d+"));
input = "abc";
oLog.error(input+" result : "+input.matches("-?\\d+\\.?\\d+"));
input = "-180";
oLog.error(input+" result : "+input.matches("-?\\d+\\.?\\d+"));
input = "8900";
oLog.error(input+" result : "+input.matches("-?\\d+\\.?\\d+"));

resulted in/output:

-19a0 result : false
190.01 result : true
abc result : false
-180 result : true
8900 result : true

November 21, 2018 - 12:38pm
Response to Chithra

These are the results from last snip.

  • Allows decimal
  • **Fails whole numbers
  • **Fails negative numbers
  • Fails alpha which is good

I have also tried another way.

But still having trouble.

Attached are my new results.

 

 

November 21, 2018 - 3:24pm
Response to Chithra

I tried this and it appears to work great!

try {
            new BigDecimal(input);
        } catch (NumberFormatException e) {
            return false;
        }
        return true;