0

I've a requirement on Regex like below for U.S. Currency (without ,).

If the user enter amount in TextBox control then it should Auto format with below expected format.

If value is 1111 - then Auto format with $ 1111.00

If value is $1111 - then Auto format with $ 1111.00

If value is $ 1111 - then Auto format with $ 1111.00

If value is 1111.0 - then Auto format with $ 1111.00

If value is $1111.00 - then Auto format with $ 1111.00

I tried like below and it is not working.

\$?\s?(\d+(?:\.\d{1,2})?)

Format: $ $1

Any suggestion please.

Soviut
  • 88,194
  • 49
  • 192
  • 260
Prasad Kanaparthi
  • 6,423
  • 4
  • 35
  • 62

1 Answers1

-1

Don't bother with a regex since this isn't a string matching or searching problem. Instead, write a function that does the following instead:

  • Strip all whitespace characters and $ characters by replacing them with '' nothing
  • Split the resulting string on .
  • Take your two tokens from the split and name them dollars and cents
  • Take the cents value and pad it with 2 0s (there are dozens of string padding algorithms out there)
  • Recombine the string with the currency symbol, a space, dollars, a decimal, and the padded cents
  • Reapply it to the textbox value

Call this function each time the textbox changes.

UPDATE: If you're targeting the most recent browsers, you can take advantage of the Intl library. It has facilities for handling internationalization and localization of strings, numbers, dates and currencies.

To decide if you can use the Intl library, check this support chart

http://caniuse.com/#feat=internationalization

Soviut
  • 88,194
  • 49
  • 192
  • 260
  • No, don't write a function. Use a library, such as `Intl`. –  Aug 07 '17 at 07:38
  • @torazaburo feel free to write your own answer. I proposed a function because we don't know the scope of OP's problem. SO had a real problem with "just use JQuery" a few years back and I don't want to repeat that by making library/frameworks recommendations over solutions to the immediate problem. – Soviut Aug 07 '17 at 07:41
  • Additionally, support for the Intl library is relatively recent especially in the mobile browser space. http://caniuse.com/#feat=internationalization I've added this to the answer. – Soviut Aug 07 '17 at 07:44
  • Thanks for adding that. Looking at the support chart you kindly included, I'm wondering what kind of browser support requirements would motivate anyone **not** to use the lib. –  Aug 07 '17 at 08:49
  • @torazaburo it's only very recently (less than 1 year as of this post) supported on mobile which accounts for at least 50%+ of most site traffic these days. Having it crash on handhelds is a really bad user experience. – Soviut Aug 07 '17 at 09:07
  • I don't see that from the support chart. What handheld is it going to crash on? –  Aug 07 '17 at 10:07
  • @torazaburo Anyone running Safari 9 on either iOS or desktop. Anyone with a 2 year old feature phone running Android, etc. – Soviut Aug 07 '17 at 17:47