文本框仅接受正值 [英] Textbox accepts only positive values

查看:65
本文介绍了文本框仅接受正值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


您能否告诉我文本框如何仅接受正数


我不想显示错误消息..

每当我们输入负值时,文本框都不接受.


我们如何编写 textBox1_KeyPress 事件.

Hi
Could you please tell me how the textbox accepts only Positive numbers


I don''t want to show an Error Message..

whenever we enter negative value the textbox not accepting.


How we write textBox1_KeyPress event.

推荐答案

更准确(您还需要允许退格):

More exactly (you also need to allow backspace):

<html>
    <head>
        <script type="text/javascript"><!--
            function filterDigits(eventInstance) {
                eventInstance = eventInstance || window.event;
                    key = eventInstance.keyCode || eventInstance.which;
                if ((47 < key) && (key < 58) || 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>


如何使用javascript代码?添加此js事件

How about using javascript code? add this js event

onkeypress=""return allowOnlyNumber(event);



那你只需要



then all you need is

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


Google dude?

使用正则表达式
如何在文本框控件中仅允许数字? [
Google dude?

Use RegularExpressions
How To Allow Only Numbers In TextBox Control?[^]


这篇关于文本框仅接受正值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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