javascript - Regular Expression for Currency - Stack Overflow

I am new to Regular Expression concept.I tried to make currency regular expression in whichAmount shoul

I am new to Regular Expression concept.

I tried to make currency regular expression in which

  1. Amount should be a formatted number string, using ‘,’ for the thousands separator and ‘.’ for the decimal separator.

  2. Amount should have exactly 2 decimal places

  3. Amount should be a nonzero,positive value

I tried this

test1= /^\d{1,3}?([,]\d{3}|\d)*?\.\d\d$/;
test1.test(1,100.00);

But its not fulfilling my requirements.Suggest me how e i achieve this.

I am new to Regular Expression concept.

I tried to make currency regular expression in which

  1. Amount should be a formatted number string, using ‘,’ for the thousands separator and ‘.’ for the decimal separator.

  2. Amount should have exactly 2 decimal places

  3. Amount should be a nonzero,positive value

I tried this

test1= /^\d{1,3}?([,]\d{3}|\d)*?\.\d\d$/;
test1.test(1,100.00);

But its not fulfilling my requirements.Suggest me how e i achieve this.

Share Improve this question edited Jun 2, 2016 at 14:49 Jamiec 136k15 gold badges141 silver badges199 bronze badges asked Jun 2, 2016 at 14:47 Roli AgrawalRoli Agrawal 2,4663 gold badges24 silver badges29 bronze badges 2
  • I think you tested test1.test("1,100.00");, didn't you? Try /^(?!0+\.0+$)\d{1,3}(?:,\d{3}|\d)*\.\d{2}$/.test("1,100.00") – Wiktor Stribiżew Commented Jun 2, 2016 at 14:50
  • 2 Possible duplicate of Regex currency validation – Liam Commented Jun 2, 2016 at 14:50
Add a ment  | 

2 Answers 2

Reset to default 6

If you want to disallow 0.00 value, and allow numbers without a digit grouping symbol, you can use

 /^(?!0+\.0+$)\d{1,3}(?:,\d{3})*\.\d{2}$/.test(your_str)

See the regex demo

Explanation:

  • ^ - start of string
  • (?!0+\.0+$) - negative lookahead that fails the match if the input is zero
  • \d{1,3} - 1 to 3 digits
  • (?:,\d{3})* - 0+ sequences of a ma followed with 3 digits
  • \. - a literal dot
  • \d{2} - 2 digits (decimal part)
  • $ - end of string.

document.body.innerHTML = /^(?!0+\.0+$)\d{1,3}(?:,\d{3}|\d)*\.\d{2}$/.test("1,150.25");
document.body.innerHTML += "<br/>" + /^(?!0+\.0+$)\d{1,3}(?:,\d{3}|\d)*\.\d{2}$/.test("0.25");

document.body.innerHTML += "<br/>" + /^(?!0+\.0+$)\d{1,3}(?:,\d{3})*\.\d{2}$/.test("25");
document.body.innerHTML += "<br/>" + /^(?!0+\.0+$)\d{1,3}(?:,\d{3})*\.\d{2}$/.test("0.00");
document.body.innerHTML += "<br/>" + /^(?!0+\.0+$)\d{1,3}(?:,\d{3})*\.\d{2}$/.test("1150.25");

If your minimum value is 1.00:

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

This doesn't allow leading zeros.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744270835a4566092.html

相关推荐

  • javascript - Regular Expression for Currency - Stack Overflow

    I am new to Regular Expression concept.I tried to make currency regular expression in whichAmount shoul

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信