Regex for Money

前端 未结 4 911
滥情空心
滥情空心 2020-12-01 18:52

I have asp:TextBox to keep a value of money, i.e. \'1000\', \'1000,0\' and \'1000,00\' (comma is the delimiter because of Russian standard).

What

相关标签:
4条回答
  • 2020-12-01 18:53

    i used this one in javascript: may be of use for you in c#

    var entered = '10.00';
    var regex = /^\d+(?:\.\d{2})?$/; // starts with N digits optional ".\d\d"
    console.log(entered.match(regex));
    
    0 讨论(0)
  • 2020-12-01 18:57
    \d+(,\d{1,2})?
    

    will allow the comma only when you have decimal digits, and allow no comma at all. The question mark means the same as {0,1}, so after the \d+ you have either zero instances (i.e. nothing) or one instance of

    ,\d{1,2}
    

    As Helen points out correctly, it will suffice to use a non-capturing group, as in

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

    The additional ?: means that the parentheses are only meant to group the ,\d{1,2} part for use by the question mark, but that there is no need to remember what was matched within these parenthesis. Since this means less work for the regex enginge, you get a performance boost.

    0 讨论(0)
  • 2020-12-01 19:15

    We use this very liberal regular expression for money validation:

    new Regex(@"^\-?\(?\$?\s*\-?\s*\(?(((\d{1,3}((\,\d{3})*|\d*))?(\.\d{1,4})?)|((\d{1,3}((\,\d{3})*|\d*))(\.\d{0,4})?))\)?$");
    

    It allows all these: $0, 0, (0.0000), .1, .01, .0001, $.1, $.01, $.0001, ($.1), ($.01), $(.0001), 0.1, 0.01, 0.0001, 1., 1111., 1,111., 1, 1.00, 1,000.00, $1, $1.00, $1,000.00, $ 1.0000, $ 1.0000, $ 1,000.0000, -1, -1.00, -1,000.00, -$1, -$1.00, -$1,000.00, -$ 1, -$ 1.00, -$ 1,000.00, $-1, $-1.00, $-1,000.00, $(1), $(1.00), $(1,000.00), $ (1), $ (1.00), $ (1,000.00), ($1), ($1.00), ($1,000.00)

    0 讨论(0)
  • 2020-12-01 19:16

    http://regexlib.com/Search.aspx?k=money

    0 讨论(0)
提交回复
热议问题