【问题标题】:How to restrict typing hyphen into extjs numberfield?如何限制在 extjs 数字字段中输入连字符?
【发布时间】:2012-06-01 09:41:51
【问题描述】:

目前 extjs numberfield 允许在字段中使用除数字以外的“-”连字符。如何限制打字?如果我提供自定义 vtype 验证,它只会在我提交后检查。

【问题讨论】:

    标签: extjs4 extjs4.1


    【解决方案1】:

    使用 autoStripChars 并删除 initComponent 中允许的连字符。更正的代码如下。

    autoStripChars: true,
    initComponent: function() {
        var me = this,
            allowed;
    
        me.callParent();
    
        me.setMinValue(me.minValue);
        me.setMaxValue(me.maxValue);
    
        // Build regexes for masking and stripping based on the configured options
        if (me.disableKeyFilter !== true) {
            allowed = me.baseChars + '';
            if (me.allowDecimals) {
                allowed += me.decimalSeparator;
            }
    /* removed code
        if (me.minValue < 0) {
                allowed += '-';
            } 
    */
            allowed = Ext.String.escapeRegex(allowed);
            me.maskRe = new RegExp('[' + allowed + ']');
            if (me.autoStripChars) {
                me.stripCharsRe = new RegExp('[^' + allowed + ']', 'gi');
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      您需要创建自定义 VType。在文本字段发生任何更改后调用 VType 验证,您可以配置允许输入的字符:

      http://docs.sencha.com/ext-js/4-0/#!/api/Ext.form.field.VTypes

      查看xxxMask 属性。

      【讨论】:

        【解决方案3】:

        您还可以使用带有 maskRe 配置的常规文本字段。

        maskRe: /[0-9]/
        

        【讨论】:

          【解决方案4】:

          类似于@lagnat 的回复,这是一个白名单,您可以使用 stripCharsRe 配置将字符列入黑名单。

          stripCharsRe: /-/
          

          如果你想限制负值。

          minValue: 0
          

          【讨论】:

            【解决方案5】:
            {
                minValue: 0,
                autoStripChars: true,
                allowExponential: false
            }
            

            【讨论】:

              猜你喜欢
              • 2012-01-29
              • 1970-01-01
              • 2015-03-31
              • 1970-01-01
              • 2012-04-08
              • 1970-01-01
              • 2018-11-07
              • 1970-01-01
              • 2014-10-14
              相关资源
              最近更新 更多