2010-11-29 64 views
2

我想實現的textBox中,用戶只能在圖形中插入文字是這樣的:面膜在多行TextBox

 

dddddddddd, 
dddddddddd, 
dddddddddd, 
... 

其中d是一個數字。如果用戶在一行中使用少於10位的數字進行控制,驗證應該失敗,並且他不應該能夠在一行中寫入多於10位的數字,那麼可接受的應該只有逗號「,」。

感謝您的幫助

回答

1
Match m = Regex.Match(textBox.Text, @"^\d{10},$", RegexOptions.Multiline); 

還沒有嘗試過,但它應該工作。請參閱herehere瞭解更多信息。

+0

而在TextBox控件,我應該在哪裏設置? – gruber 2010-11-29 09:45:52

1

我建議正則表達式

\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"); 

請注意,你需要使用一個逐字字符串( @"...")或將所有反斜槓加倍正則表達式。