When a user submits a form I need to make sure that the input contains at least a minimum number of digits. The problem is that I don't know what format the input will be in. The digits likely won't be in a row, and might be separated by letters, punctuation, spaces, etc. I don't care about the rest of the string.
当用户提交表单时,我需要确保输入包含至少最小位数。问题是我不知道输入的格式是什么。数字可能不会连续,可能用字母,标点符号,空格等分开。我不关心其余的字符串。
I'd like to check this with a RegularExpressionValidator but I'm not quite sure how to write the regex.
我想用RegularExpressionValidator检查这个,但我不太清楚如何编写正则表达式。
I guess this would be similar to a phone number regex, but a phone number at least has some common formats.
我想这类似于电话号码正则表达式,但电话号码至少有一些常见的格式。
3 个解决方案
#1
12
The following will match an input string containing at least n
digits:
以下内容将匹配包含至少n位数的输入字符串:
Regex.IsMatch(input, @"(\D*\d){n}");
where n
is an integer value.
其中n是整数值。
A short explanation:
一个简短的解释:
-
\D*
matches zero or more non-digit chars (\D
is a short hand for[^0-9]
or[^\d]
); - so
\D*\d
matches zero or more non-digit chars followed by a digit; - and
(\D*\d){n}
groups, and repeats, the previousn
times.
\ D *匹配零个或多个非数字字符(\ D是[^ 0-9]或[^ \ d]的简写);
所以\ D * \ d匹配零个或多个非数字字符后跟一个数字;
和(\ D * \ d){n}组,并重复,前n次。
#2
3
I would approach it something like this:
我会接近这样的事情:
Regex.IsMatch(input, @"^(.*[0-9].*){10}$");
In words, this would search for 10 digits, each of which are surrounded by 0 or more characters. Since the .* are greedy, any additional digits would be matched by them as well.
换句话说,这将搜索10个数字,每个数字被0个或更多个字符包围。由于。*是贪婪的,所以任何额外的数字也会被它们匹配。
Anyway, check out http://regexlib.com/RETester.aspx It can be really hard to write a regular expression without something to test against.
无论如何,请查看http://regexlib.com/RETester.aspx如果没有要测试的东西,编写正则表达式真的很难。
#3
0
in order to have at least n digits, you need to use following regex:
要获得至少n位数,您需要使用以下正则表达式:
(\D*\d){n,}
Regex (\D*\d){n} will match exactly n digits.
正则表达式(\ D * \ d){n}将恰好匹配n位数。
Regards, Karlo.
#1
12
The following will match an input string containing at least n
digits:
以下内容将匹配包含至少n位数的输入字符串:
Regex.IsMatch(input, @"(\D*\d){n}");
where n
is an integer value.
其中n是整数值。
A short explanation:
一个简短的解释:
-
\D*
matches zero or more non-digit chars (\D
is a short hand for[^0-9]
or[^\d]
); - so
\D*\d
matches zero or more non-digit chars followed by a digit; - and
(\D*\d){n}
groups, and repeats, the previousn
times.
\ D *匹配零个或多个非数字字符(\ D是[^ 0-9]或[^ \ d]的简写);
所以\ D * \ d匹配零个或多个非数字字符后跟一个数字;
和(\ D * \ d){n}组,并重复,前n次。
#2
3
I would approach it something like this:
我会接近这样的事情:
Regex.IsMatch(input, @"^(.*[0-9].*){10}$");
In words, this would search for 10 digits, each of which are surrounded by 0 or more characters. Since the .* are greedy, any additional digits would be matched by them as well.
换句话说,这将搜索10个数字,每个数字被0个或更多个字符包围。由于。*是贪婪的,所以任何额外的数字也会被它们匹配。
Anyway, check out http://regexlib.com/RETester.aspx It can be really hard to write a regular expression without something to test against.
无论如何,请查看http://regexlib.com/RETester.aspx如果没有要测试的东西,编写正则表达式真的很难。
#3
0
in order to have at least n digits, you need to use following regex:
要获得至少n位数,您需要使用以下正则表达式:
(\D*\d){n,}
Regex (\D*\d){n} will match exactly n digits.
正则表达式(\ D * \ d){n}将恰好匹配n位数。
Regards, Karlo.