正则表达式的问题,谢谢各位!

i8210 2005-08-08 09:04:02
俺的一个正则表达式:[0-9]{0,10}
俺想要做到只能输入10位数字,可现在还能输入10位空格,不知咋该。

特地求教,谢谢诸位!
...全文
226 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
i8210 2005-08-11
  • 打赏
  • 举报
回复
to: fancyf(凡瑞)

再用一个RequiredFieldValidator,就可以避免只输空格了
============================================================

我的那些textbox不都是必須入力字段。



但是問題現在問題已經被公司的leader解決了。謝謝各位。
特別感謝:fancyf(凡瑞)
感謝你給了我解決問題的方向。
linuxyf 2005-08-10
  • 打赏
  • 举报
回复
bool FoundMatch = false;
try {
FoundMatch = Regex.IsMatch(textBox1.Text, "^(\\d{1,10})$");
} catch (ArgumentException ex) {
// Syntax error in the regular expression
}
linuxyf 2005-08-10
  • 打赏
  • 举报
回复
bool FoundMatch = false;
try {
FoundMatch = Regex.IsMatch(SubjectString, "^(\\d{1,10})$");
} catch (ArgumentException ex) {
// Syntax error in the regular expression
}
cdo 2005-08-10
  • 打赏
  • 举报
回复
string strPattarn = @"^(\d{1,10})$";
Regex reg = new Regex(strPattarn);
if (reg.IsMatch(textBox1.Text))
{
MessageBox.Show(this, "OK");
}
Toti 2005-08-10
  • 打赏
  • 举报
回复
如果俺不想让RegularExpressionValidator自动trim空格,那该怎么做?
我想你只要用RegularExpressionValidator验证控件,控件就会自动处理掉首尾的空格
,我想要不让它处理掉有点不大可能
itmingong 2005-08-10
  • 打赏
  • 举报
回复
up
lh8287 2005-08-10
  • 打赏
  • 举报
回复
cchon(中文字符)应该是正确的

测试reg,建议使用RegexDesigner.NET来测试,很方便的
i8210 2005-08-10
  • 打赏
  • 举报
回复
up
i8210 2005-08-10
  • 打赏
  • 举报
回复
谢谢各位!

如果俺不想让RegularExpressionValidator自动trim空格,那该怎么做?谢谢!
fanruinet 2005-08-10
  • 打赏
  • 举报
回复
再用一个RequiredFieldValidator,就可以避免只输空格了
cchon 2005-08-09
  • 打赏
  • 举报
回复
string strPattarn = @"^(\d{1,10})$";
Regex reg = new Regex(strPattarn);
if (reg.IsMatch(textBox1.Text))
{
MessageBox.Show(this, "OK");
}
linuxyf 2005-08-09
  • 打赏
  • 举报
回复
^[0-9]{10}$

^ Assert position at the start of the string
[0-9] Match a single character out of the list: in the range between 0 and 9 ォ[0-9]{10}サ
{10} Exactly 10 times ォ{10}
$ Assert position at the end of the string (or before the line break at the end of the string, if any)

搂主,这个是没有错的,如果有错,那应该是你用法的问题
i8210 2005-08-09
  • 打赏
  • 举报
回复
up
i8210 2005-08-09
  • 打赏
  • 举报
回复
俺快疯了

^[0-9]+$ 纯空格还是可以通过啊
silverseven7 2005-08-09
  • 打赏
  • 举报
回复
public static bool IsDecimail(string decItem)
{
try
{
decimal.Parse(decItem);
}
catch
{
return false;
}

return true;
}

private const string IS_NUMBER = @"(^[0-9]+$)";
private const string IS_YYYYMM_ICON = @"(^[1][8-9][0-9][0-9]/[0][1-9]+$)|(^[1][8-9][0-9][0-9]/[1][0-2]+$)|(^[2-9][0-9][0-9][0-9]/[0][1-9]+$)|(^[2-9][0-9][0-9][0-9]/[1][0-2]+$)";
private const string IS_YYYYMM = @"(^[1][8-9][0-9][0-9][0][1-9]+$)|(^[1][8-9][0-9][0-9][1][0-2]+$)|(^[2-9][0-9][0-9][0-9][0][1-9]+$)|(^[2-9][0-9][0-9][0-9][1][0-2]+$)";
private const string IS_HALF_WORD = @"(^(([!-/:-@[-`{-~])|([a-zA-Z0-9]))+$)";
i8210 2005-08-09
  • 打赏
  • 举报
回复
可能是俺没表达清楚,[0-9]{0,10} 这个表达式不光能输入数字而且还能输入纯空格,这是俺所不理解的。
俺想让它只能输入数字!

谢谢啊
fanruinet 2005-08-09
  • 打赏
  • 举报
回复
^[0-9]{0,10}$这个表达式本身没问题,它不允许输入空格
但是仅用RegularExpressionValidator的话首尾的空格会被trim掉,全是空格相当于没有输入,这时正则表达式不起作用
cchon 2005-08-08
  • 打赏
  • 举报
回复
\d{1,10}
fanruinet 2005-08-08
  • 打赏
  • 举报
回复
只能输入10位数字:^[0-9]{10}$
输入0-10位数字:^[0-9]{0,10}$

关键是^和$

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧