如何对文本框使用文本和数字验证 [英] how to use text and numeric validation to textbox

查看:115
本文介绍了如何对文本框使用文本和数字验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用c#创建了一个Windows窗体,因为我想对文本框应用验证,即仅输入文本或仅输入数字值.
请帮助我.

在此先感谢.

I created a windows form in c# ,in that for textbox I want to apply validation i.e Only to enter the text or to enter only numeric values.
please help me in this.

Thanks in Advance.

推荐答案

您提到Javascript很好.最好在客户端执行此操作,因为您必须消耗的事件过于频繁.

方法如下:
That''s good you mention Javascript. It''s the best to do it on the client side, as the events you have to consume are too frequent.

Here is how:
<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



Pay attention that backspace is also allowed. By some historical reasons backspace is considered a character and should be allowed anyway.

—SA


修复您在混淆中使用的标签.您是说C#和JavaScript还是在谈论winforms或ASP.Net.
如果您正在寻找ASP.Net验证,请参见此处 [ ^ ].
Firt the tag you''ve used in confusing. Are you talking about winforms or ASP.Net as you say C# and JavaScript.
If you are looking for ASP.Net validations, see here[^].


在按键事件中,用char.IsDigit检查每个键,然后决定要做什么.
In keypress event check each key with char.IsDigit and decide what to do.


这篇关于如何对文本框使用文本和数字验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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