C#怎样实现文本框只能输入只能输入0~100数字。

管理大亨 2007-07-23 10:58:35
C#怎样实现文本框只能输入只能输入0~100数字。
...全文
1172 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
cyclone_ 2007-09-04
  • 打赏
  • 举报
回复
if ((Convert.ToInt32(TextBox1.Text.Trim().ToString()) / 101) >= 1)
{
Response.Write("<script>alert('请输入0~100之间的数字');</script>");
}
else
{
Response.Write("<script>alert('输入的数字是:" + TextBox1.Text.Trim().ToString() + "');</script>");
}

这样算嘛?
zhuqueta0101 2007-09-03
  • 打赏
  • 举报
回复
貌似有点问题

你这是验证e.keychar,一个字符

(@"^(\d{0,2} ¦100)$"); 这个表达式是验证字符串?
ll124993513 2007-08-28
  • 打赏
  • 举报
回复


using system.text.regularexpressions;

然后在textbox的keypress的事件中添加如下代码:
if(convert.toint32(e.keychar)==8)
{
e.handle=false;
}
else
{
regex numregex=new regex(@"^(\d{0,2} ¦100)$");
match result=numregex.match(convert.tostring(e.keychar));
if(result.success)
{
e.handled=false;
}
else
{
e.handled=true;
}
}
}
QQR_007 2007-08-26
  • 打赏
  • 举报
回复
try
{
convert.todouble(textbox.text)
}
catch(转换出错类型)
{
}
zs621 2007-08-01
  • 打赏
  • 举报
回复
up
cuiweifu 2007-08-01
  • 打赏
  • 举报
回复
好像记得AJAX控件里有专门干这个的
ll124993513 2007-07-31
  • 打赏
  • 举报
回复


然后在textbox的keypress的事件中添加如下代码:
if(convert.toint32(e.keychar)==8)
{
e.handle=false;
}
else
{
regex numregex=new regex(@"^(\d{0,2}|100)$");
match result=numregex.match(convert.tostring(e.keychar));
if(result.success)
{
e.handled=false;
}
else
{
e.handled=true;
}
}
}

yzl1982 2007-07-30
  • 打赏
  • 举报
回复
在WINFORM的操作:
首先引用:
using system.text.regularexpressions;

然后在textbox的keypress的事件中添加如下代码:
if(convert.toint32(e.keychar)==8)
{
e.handle=false;
}
else
{
regex numregex=new regex(@"^(\d{0,2}|100)$");
match result=numregex.match(convert.tostring(e.keychar));
if(result.success)
{
e.handled=false;
}
else
{
e.handled=true;
}
}
}
DareDevillll 2007-07-30
  • 打赏
  • 举报
回复
asc吧,明显不是web form
小码 2007-07-29
  • 打赏
  • 举报
回复
<SCRIPT LANGUAGE="JavaScript">
function numericCheck(){
nr1=document.myform.content.value;
flg=0;
str="";
spc=""
arw="";
for (var i=0;i<nr1.length;i++){
cmp=".0123456789"
tst=nr1.substring(i,i+1)
if (cmp.indexOf(tst)<0){
flg++;
str+=" "+tst;
spc+=tst;
arw+="^";
}
else{arw+="_";}
}
if (flg!=0){
if (spc.indexOf(" ")>-1) {
str+="和空格";
}
alert("这里只接受数字,发现"
+flg+"个不可接受的字符:\n"+str);
return false;
}
}
</SCRIPT>
<form name="myform" method="post" enctype="text/plain" onsubmit="return numericCheck()" action="mailto:mail@mail.com">
<INPUT TYPE="text" name="content">
<INPUT TYPE="submit" NAME="submit" VALUE="递交">
</form>
MicroSoftor 2007-07-24
  • 打赏
  • 举报
回复
如果是WEB
用正则来控制最方便了
正则为:^(\d{0,2}|100)$
限制输入0-100

如果是winFrom,那么在文本框的焦点离开时候来判断
guoweijun394 2007-07-23
  • 打赏
  • 举报
回复
用正则 或者判断ascII 码

1,979

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 其他语言讨论
社区管理员
  • 其他语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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