2017-04-11 67 views
-1

如何在文本字段上的每2个字符后添加点(。)。在文本字段上输入每2个字符后添加点数extjs 5

有没有具体的方法来做到这一点?

我的代码:

var txtNoNpwp = { 
    afterLabelTextTpl: required, 
    allowBlank: false, 
    anchor: '100%', 
    labelAlign: 'left', 
    labelWidth: 130, 
    fieldLabel: 'No NPWP', 
    id: 'txtNoNpwp', 
    name: 'txtNoNpwp', 
    xtype: 'textfield' 
}; 
+0

您可以捕获keyup事件并在每个第二个字符后面添加'.'。我想这是我能够在没有看到你的代码的情况下做到的。请分享您的代码和**您在哪里卡住** – Rajesh

+0

这可能有所帮助:https://www.sencha.com/forum/showthread.php?106101-Keyup-on-a-TextField-SearchFIeld – Rajesh

+0

谢谢@Rajesh我将尝试 – andrei

回答

1

可以在change事件的值更改为所需的格式。 每当该字段的值发生更改时,都会使用新值和旧值调用它。

首先使用以下代码删除以前的格式。

newValue = newValue.replace(/\./g, ''); 

然后应用新的格式。

newValue = newValue.replace(/.{2}/g, function (substr) { 
    return substr + '.' 
}); 

最后,你必须与

field.setRawValue(newValue); 

完整的代码设置新的值应该是这样的,看到working Sencha Fiddle

var txtNoNpwp = { 
    listeners: { 
     change: function (field, newValue) { 
      newValue = newValue.replace(/\./g, ''); // remove previous format 
      newValue = newValue.replace(/.{2}/g, function (substr) { // apply new format 
       return substr + '.' 
      }); 
      field.setRawValue(newValue); 
     } 
    }, 
    xtype: 'textfield', 
    //... 
}; 
+0

这个答案最适合我。十分感谢 ! @安迪-Y – andrei

相关问题