如何防止在asp.net的文本框中输入字母 [英] How to prevent entering letters in a textbox in asp.net

查看:78
本文介绍了如何防止在asp.net的文本框中输入字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp格式的文本框,我只想输入数字和.".
我想创建一个会话以将文本框中的文本保存在其中以在另一页中使用它
谢谢您的帮助.

I have a textbox in a asp form and I want only to input numbers and "."
And I want to create a session to save in it the text in the text box to use it in another page
Thank you for your help

推荐答案

您应该使用Javascript来完成此操作,因为为此所需的事件太频繁了,并且无法通过服务器来进行. >
看这个例子:
You should do it in Javascript as the event needed for this is too frequent, too costly to do it through the server.

Look at this sample:
<html>
   <head>
      <script type="text/javascript"><!--
         function filterDigits(eventInstance) { 
            eventInstance = eventInstance || window.event;
                key = eventInstance.keyCode || eventInstance.which;
            if ((47 < key) && (key < 58) || key = 45 || key == 8) {
               return true;
            } else {
                    if (eventInstance.preventDefault) eventInstance.preventDefault();
                    eventInstance.returnValue = false;
                    return false;
                } //if
         } //filterDigits
      --></script>
   </head>
<body">

<input type="text" onkeypress="filterDigits(event)"/>

</body>
</html>



它的作用恰恰是这样:允许数字和.'',但也可以使用退格键.由于某些历史原因,退格键是作为字符而不是原始键盘事件处理的,因此应单独允许.

-SA



It does exactly this: allows digits and ''.'' but also a backspace. By some historical reasons, backspace is processed as a character, not as a raw keyboard event, so it should be allowed separately.

—SA


存档 [ ^ ]可能会有所帮助.

IsNumeric可能是最好的选择.
参见此处 [
The Archive[^] may help.

IsNumeric is probably the best way to go though.
See Here[^]


Hope they help.


Mark up if they do!


很多方法来实现
1)来自javacript
2)通过jquery
3)使用Ajax控制套件验证器

或只在ASP中使用compare validateatior(在您的情况下使用double作为类型)

lots of ways to do that
1) from javacript
2) via jquery
3) using ajax control kit validators

or simply use compare validatior in asp (use double as a type in your case)

<br />
<asp:comparevalidator operator="DataTypeCheck" type="Double" controltovalidate="MyTextBox" display="None" errormessage="Valid Number is required." runat="server" /><br />



使用文本框值作为查询参数重定向到另一个页面,例如:



redirect to another page using text box value as a query parameter like:

<br />
Response.Redirect("~/yourPage.aspx?value=" + MyTextBox.Text);<br />



将其存储在服务器端的会话中,如下所示:


OR
store it in a session on server side like below:

<br />
Session["TBValue"] = MyTexyBox.Text;<br />


这篇关于如何防止在asp.net的文本框中输入字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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