Question
Veteran Careers First
US
Last activity: 21 Nov 2018 15:49 EST
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();
-
Like (0)
-
Share this page Facebook Twitter LinkedIn Email Copying... Copied!
Accepted Solution
Veteran Careers First
US
I tried this and it appears to work great!
try {
new BigDecimal(input);
} catch (NumberFormatException e) {
return false;
}
return true;
Pegasystems Inc.
US
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.
Veteran Careers First
US
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.
Pegasystems Inc.
US
You could try this in a method since it considers "-" as optional and must have at least one digit:
Veteran Careers First
US
When you say "method" could you explain...
Would I use this in a when rule, activity method, or validate rule.
Veteran Careers First
US
or if you mean to add it to function can you show it all together
Veteran Careers First
US
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.
Pegasystems Inc.
US
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!
Veteran Careers First
US
- 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
- 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
Pegasystems Inc.
US
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
Veteran Careers First
US
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.
Accepted Solution
Veteran Careers First
US
I tried this and it appears to work great!
try {
new BigDecimal(input);
} catch (NumberFormatException e) {
return false;
}
return true;