-1

I need to validate a field that can be inputed as CURRENCY, or NUMERIC, or PERCENTAGE. I haven't seen any posts for ALL 3 conditions at once so wanted to ask the community.

I found the following code on here (thanks Gary!!) that works great for validating currency or numerics at the same time.
(?=.)^\$?(([1-9][0-9]{0,2}(,[0-9]{3})*)|[0-9]+)?(\.[0-9]{1,2})?$

Regex currency validation

How do you update this expresssion to validate for percentages as well? Basically I would like to allow a percent symbol at the very end IF
- they don't enter a dollar sign
- (if possible I'd like to also validate that if they enter a percent symbol then the value can't be over 100, but I can live w/o this validation)

Gary
  • 13,303
  • 18
  • 49
  • 71
  • 1
    StackOverflow expects you to [try to solve your own problem first](http://meta.stackoverflow.com/questions/261592), and we also [don't answer homework questions](https://softwareengineering.meta.stackexchange.com/questions/6166) (ignore if you're not asking about hw). Please update your question to show what you have already tried in a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve). For further information, please see [how to ask good questions](http://stackoverflow.com/help/how-to-ask), and take the [tour of the site](http://stackoverflow.com/tour) :) – giorgiga Feb 06 '18 at 20:47
  • What makes a number *valid*? Do you allow negative values, thousands separators, decimals? Is there a limit on number of decimals? Are numbers valid with a `+` before them or just optional `-`? What currency symbols do you want to support `$ ؋ ƒ ₡ ¥ € £ ¢ ₪ ﷼ ₩ ₮`, etc.? Basically, what does each possible value/value type consist of in terms of rules? Also, what have you tried? – ctwheels Feb 06 '18 at 20:51
  • I tried the following and it works well (?=.)^\$?(([1-9][0-9]{0,2}(,[0-9]{3})*)|[0-9]+)?(.[0-9]{1,2})?$ @ctwheels this syntax handles/answers all your questions (no negatives, only $ allowed for currency) The part I'm struggling with and can't figure out is how do I embed Percentage validation into the same syntax as my Currency Validation – Michael Shrader Feb 06 '18 at 21:19
  • @giorgiga I appreciate the feedback. I did research and tinker before asking the question. And this is definitely not homework. I'm a Business Intelligence developer and know very little about java or RegEx (hence my question) And I did provide an example and take a tour of the site. – Michael Shrader Feb 06 '18 at 21:28
  • Doesn't seem so, Micheal. Your question reads "I need to do such and such. All I have is some copypasted code that does something similar. Can you do it for me?" If your point is you provided additional info as comments, you should edit your question adding that info in - stackoverlow works this way and people rarely read comments - eg: I didn't read them all :) – giorgiga Feb 06 '18 at 21:49

1 Answers1

1

I might think of a cleaner way to do it later, but for now this should work

(?!\$.+?%)(?=.*\d)^\$?(?:(?:0*[1-9]\d{0,2}(?:,\d{3})*)|0+\d*)?(?:\.\d{1,2})?%?$

It simply takes the regex you borrowed from me and adds

  • An optional '%' at the end (in addition to the already optional '$' at the beginning): %?.
  • A negative lookahead to check that it doesn't have both: (?!\$.+?%)
  • A positive lookahead to ensure that it has one or the other (since they were both optional): (?=\$.+|.+%) make sure there's a number somewhere (since everything is individually optional): (?=.*\d)
Gary
  • 13,303
  • 18
  • 49
  • 71
  • Thanks Gary! This def works but I should've been more clear in my requirements as I still want numbers to be valid as well (which your original syntax allowed). So $5, 5, and 5% should alll be valid. I think i can remove the postive lookahead you added at that should resolve it. – Michael Shrader Feb 06 '18 at 21:39
  • 2
    LOL Gary answered the question! – ctwheels Feb 06 '18 at 21:39
  • @MichaelShrader In that case, just remove the positive lookahead in the third step. – Gary Feb 06 '18 at 21:41
  • Yep I just noticed that. Thanks a bunch! – Michael Shrader Feb 06 '18 at 21:43
  • @gary Just an FYI... Your proposed syntax allows for commas in invalid places so I updated it to (?!\$.+?%)(?=.)^\$?(([1-9][0-9]{0,2}(,[0-9]{3})*)|[0-9]+)?(\.[0-9]{1,2})?%?$ This provides for the optional % at end (in addition to the optional $ at beginning) and has the negative lookahead so you can't have both. Now I just need to figure out how I can validate that you can't enter in leading zero unless it's followed immediately by a decimal. (Syntax currently allows 0001 as valid) – Michael Shrader Feb 07 '18 at 18:07
  • @MichaelShrader The invalid commas were a copy/paste error (I somehow lost the escape on my dot). I updated the answer – Gary Feb 07 '18 at 19:13