ASP.NET 货币正则表达式

ASP.NET Currency regular expression

所需的货币格式类似于

1,100,258
100,258
23,258
3,258

或所有整数如 1234562421323 等等。

我在下面输入 ValidationExpression

(^[0-9]{1,3}(,\d{3})*) | (^[0-9][0-9]*)

但是没用。

你有ignore pattern whitespace吗?如果不是,请删除管道两侧的两个空格。

由于您正在尝试匹配其中任何一个,因此您应该在字符串的末尾添加一个标记 $,就像这样

另外 ^[0-9][0-9]* 有什么意义,什么时候可以使用 ^[0-9]+

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

^(\d{1,3}(?:,\d{3})*|\d+)$

解释:

 ^                     # Anchors to the beginning to the string.
 (                     # Opens CG1
     \d{1,3}           # Token: \d (digit)
     (?:               # Opens NCG
         ,             # Literal ,
         \d{3}         # Token: \d (digit)
                         # Repeats 3 times.
     )*                # Closes NCG
                         # * repeats zero or more times
 |                     # Alternation (CG1)
     \d+               # Token: \d (digit)
                         # + repeats one or more times
 )                     # Closes CG1
 $                     # Anchors to the end to the string.