【问题标题】:NumberTextBox allow only integerNumberTextBox 只允许整数
【发布时间】:2015-07-03 06:28:43
【问题描述】:

我只需要对NumberTextBox 中的整数 值进行验证。

使用以下代码,在NumberTextBox 54.454 中输入十进制值会错误验证。

我想知道:

  • 如何仅验证 integer 值?
  • 是否可以避免用户输入.

https://jsfiddle.net/9Lh3p0fb/7/

require(["dijit/form/NumberTextBox", "dojo/domReady!"], function(NumberTextBox){
    new NumberTextBox({
          name: "programmatic",
        constraints: {pattern: '@@@'}
    }, "programmatic").startup();
});

【问题讨论】:

标签: javascript dojo dijit.form


【解决方案1】:

您可以使用places: 0 作为configuration 选项:

require(["dijit/form/NumberTextBox", "dojo/domReady!"], function (NumberTextBox) {
    new NumberTextBox({
        name: "programmatic",
        constraints: {
            pattern: '@@@',
            places: 0
        }
    }, "programmatic").startup();
});

【讨论】:

  • 只是一个简短的问题...您知道是否可以在控件本身上禁用输入 ./?使用数字文本框。我知道可以通过一些解决事件 keydown 的方法,但我不确定我的 dijit 本身是否支持它。感谢您抽出宝贵时间。
  • @GibboK,没问题,不太清楚,我赶紧看看
【解决方案2】:

我从here 得到了一个代码并修改了 .请检查它是否解决了您的问题。

<input type='text' onkeypress='validate(event)' />

<script type="text/javascript">
    function validate(evt) {
      var theEvent = evt || window.event;
      var key = theEvent.keyCode || theEvent.which;
      key = String.fromCharCode( key );
      var regex = /[0-9]/;
      if( !regex.test(key) ) {
        theEvent.returnValue = false;
        if(theEvent.preventDefault) theEvent.preventDefault();
      }
    }
</script>` 

【讨论】:

  • 感谢您的回答,但我的问题专门针对 dijit 中的 NumberTextBox ..
猜你喜欢
  • 2021-02-07
  • 2016-06-30
  • 2016-07-08
  • 2012-12-16
  • 2018-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多