gruber
gruber

Reputation: 29729

Mask in Multiline TextBox

I WOuld like to implement textBox in which user can only insert text in pattern like this:


dddddddddd,
dddddddddd,
dddddddddd,
...

where d is a digit. If user leave control with less then 10 digits in a row validation should fail and he should not be able to write in one line more than 10 digits, then acceptable should be only comma ",".

Thanks for help

Upvotes: 2

Views: 746

Answers (2)

Tim Pietzcker
Tim Pietzcker

Reputation: 336158

I suggest the regex

\A(?:\s*\d{10},)*\s*\d{10}\s*\Z

Explanation:

\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

In C#, this would look like

validInput = Regex.IsMatch(subjectString, @"\A(?:\s*\d{10},)*\s*\d{10}\s*\Z");

Note that you need to use a verbatim string (@"...") or double all the backslashes in the regex.

Upvotes: 1

npinti
npinti

Reputation: 52185

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

Haven't tried it, but it should work. Please take a look here and here for more information.

Upvotes: 1

Related Questions