asp .net页面中仅限数字的文本框 [英] Number only textbox in asp .net page

查看:57
本文介绍了asp .net页面中仅限数字的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想创建一个类

的文本框System.Web.UI.WebControls.TextBox 

并希望用户只能输入数字(数字)。



我已经搜索了很多这个,我得到的唯一结果就是使用 KeyPressEventArgs OnKeyPress 我认为是Windows Form文本框类型的方法,或者是使用Javascript实现的 onkeypress 文本框的事件。



我想知道是否有任何方法可以使用C#制作这样的文本框?



我不想验证文本框。我希望用户应该只能在文本框中输入数字。



提前致谢



问候

Abhisek Majumder

解决方案

如何使用JavaScript过滤输入。



< input name =myInputid =myInputonkeypress =return allowOnlyNumber(event);> 





  function  allowOnlyNumber(evt)
{
var charCode =(evt.which)? evt.which: event .keyCode
if (charCode> 31 &&(charCode< 48 || charCode> 57 ))
return false;
返回 true;
}





注意:在allowOnlyNumber函数中,它满足我的需要,你需要根据自己的需要进行修改。


Ajax解决方案对您有什么帮助吗?如果是,那么这个 [ ^ ]可能会有所帮助。


您可以在特定文本框中使用RegularExpression验证...验证表达式将如下

[0-9] *

Hi,

I want to create a textbox of class

System.Web.UI.WebControls.TextBox

and want that the user will be able to enter only digits(numbers).

I have searched a lot on this and the only result that I get is either using KeyPressEventArgs and OnKeyPress method which I think is for Windows Form textbox type or it is implemented using Javascript for onkeypress event of the textbox.

I want to know is there any method by which we can make such a textbox using C# only?

I don't want to validate the textbox. I want the user should be able to type only numbers in the textbox.

Thanks in advance

Regards
Abhisek Majumder

解决方案

How about using JavaScript to filter the input.

<input name="myInput" id="myInput" onkeypress="return allowOnlyNumber(event);">



function allowOnlyNumber(evt)
{
  var charCode = (evt.which) ? evt.which : event.keyCode
  if (charCode > 31 && (charCode < 48 || charCode > 57))
    return false;
  return true;
}



Note: In the allowOnlyNumber function, it serves my need, you need to modify it as it suit you.


Will an Ajax solution be of any help to you? If yes, then this[^] might help.


You can use RegularExpression Validation on the specific text box...The validation expression will be the following
[0-9]*


这篇关于asp .net页面中仅限数字的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆