1
votes

I already verified there are no paste events for textfield in extjs 4.1. But i donot want that user should be able to paste into this textfield. What other options are available that user is not allowed to paste any value in the textfield. Please note that the textfield allows only numeric values, no chars/special chars or alphabets. Below is the code snippet that i have as of now.

{
                                  xtype:"textfield",    
                                  fieldLabel: 'Debit Account',
                                  name:'debitAccount',
                                  id : 'debitacct',
                                  enableKeyEvents:true,
                                  maskRe: /[0-9]/,
                                  allowBlank: false,
                                  allowNegative: false,
                                  maxLength: 9,
                                  enforceMaxLength:true,
                                  listeners : {
                                    specialkey : function(field, e) {
                                    filterBackspaceKey(e);
                                    }
                                    }
                             }

Any help is appreciated.

3
@dbrin My bad, Thank you ! I just accepted it.SKS

3 Answers

6
votes

After i got to see the link @Sencha, the solution was easy. Code below.

{
                                  xtype:"textfield",    
                                  fieldLabel: 'Debit Account',
                                  name:'debitAccount',
                                  id : 'debitacct',
                                  enableKeyEvents:true,
                                  maskRe: /[0-9]/,
                                  allowBlank: false,
                                  allowNegative: false,
                                  maxLength: 9,
                                  enforceMaxLength:true,
                                  listeners : {
                                    specialkey : function(field, e) {
                                    filterBackspaceKey(e);
                                    },
                                    paste: {
                                        element: 'inputEl',
                                        fn: function(event, inputEl) {
                                        if(event.type == "paste"){
                                        event.preventDefault();
                                        return false;
                                        }
                                        }
                                    }
                                    }
                             }

Refer link from Sencha : http://www.sencha.com/forum/showthread.php?175253-Paste-event

1
votes
  • On any click and keydown do this:
    • Check if old value equals to new value (change event)
    • Save new value to old value
    • If content has changed, you can diff the values and if changed part was more then one character, content has been pasted -> revert to old value

Hope that helps.

0
votes

try after change your listeners to below

listeners : {
                change : function(field, newValue,oldValue) {
                    var validation=/^\d+$/;
                    if(!validation.test(newValue) && newValue!='' ) {
                        field.setValue(oldValue);
                    }
                }
            }