【发布时间】:2010-11-29 08:34:59
【问题描述】:
我想实现一个文本框,用户只能在这样的模式中插入文本:
dddddddddd, dddddddddd, dddddddddd, ...其中 d 是一个数字。如果用户在一行中以少于 10 位的数字离开控件,验证会失败,并且他不能在一行中写入超过 10 位的数字,那么可接受的应该只有逗号“,”。
感谢您的帮助
【问题讨论】:
我想实现一个文本框,用户只能在这样的模式中插入文本:
dddddddddd, dddddddddd, dddddddddd, ...其中 d 是一个数字。如果用户在一行中以少于 10 位的数字离开控件,验证会失败,并且他不能在一行中写入超过 10 位的数字,那么可接受的应该只有逗号“,”。
感谢您的帮助
【问题讨论】:
我建议使用正则表达式
\A(?:\s*\d{10},)*\s*\d{10}\s*\Z
说明:
\A # start of the string
(?: # match the following zero or more times:
\s* # optional whitespace, including newlines
\d{10}, # 10 digits, followed by a comma
)* # end of repeated group
\s* # match optional whitespace
\d{10} # match 10 digits (this time no comma)
\s* # optional whitespace
\Z # end of string
在 C# 中,这看起来像
validInput = Regex.IsMatch(subjectString, @"\A(?:\s*\d{10},)*\s*\d{10}\s*\Z");
请注意,您需要使用逐字字符串 (@"...") 或将正则表达式中的所有反斜杠加倍。
【讨论】: