cancel
Showing results for 
Search instead for 
Did you mean: 

For function e.preventDefault() i am getting error: "Undefined is not a function". Do i need to include some library?

former_member197578
Participant
0 Kudos

Hello UI5 people,

I was trying to put validation on my textfield so that it accepts only numbers. I found following code in this community an tried to use it:

var textbox = new sap.ui.commons.TextField("data",{}); 

  textbox.attachBrowserEvent("keypress",function(e){ 

  var key_codes = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 0, 8]; 

            if (!($.inArray(e.which, key_codes) >= 0)) { 

              e.preventDefault(); 

            } 

            

  }); 


But on the line "e.preventDefault" i am getting error : "Undefined is not a function"


So does this mean i need to include some library ?


I tried including sap.ui.base.Event class but it gives me error "could not load"


What do i need to do??

Please help

Thank you,

Regards,

Chetna

Accepted Solutions (1)

Accepted Solutions (1)

pfefferf
Active Contributor
0 Kudos

Hello Chetna,

your coding should work. It is enough to include the sap.ui.commons library for your case in the bootstrap tag.

I just tested it on jsfiddle (OpenUI5 TextField with Only Numbers - JSFiddle) with the newest versions of Chrome, Firefox and IE.

What browser and version you are using? Maybe you can check if the example on jsfiddle works for you too. If yes, then you can check your coding agains the example coding.

Best regards,

Florian

former_member197578
Participant
0 Kudos

Hello Florian,

Can you check the following code and tell me whats wrong :

newDvrTable.addColumn(new sap.ui.table.Column({

  label: new sap.ui.commons.Label({text: " Dealer Code", wrapping : true}),

  template: new sap.ui.commons.TextField({

  id:"dlrNum",

  value: "{DealerNo}" ,

  maxLength: 10,

  liveChange: function(oEvent) {

                  

                   

                    sap.ui.getCore().byId("dlrNum").attachBrowserEvent("keypress",function(val){

                   

                    var key_codes = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 0, 8];

               if (!($.inArray(val.which, key_codes) >= 0)) {

                 val.preventDefault();

               }

                    });

                  

                 

  },

I think this is not the way of doing it, but then how do i do it over here? textfield is a field of my table.

Thank you,

Regards,

Chetna

Answers (3)

Answers (3)

former_member197578
Participant
0 Kudos

Thank you all for responding.

Finally worked,

Best Regards,

Chetna

Former Member
0 Kudos

Copy pasted the same code . It works!!

https://jsfiddle.net/indrajithpatel/n4ub916u/

former_member197578
Participant
0 Kudos

Hello,

Actually you are right its working, there is something wrong with my code.

newDvrTable.addColumn(new sap.ui.table.Column({

  label: new sap.ui.commons.Label({text: " Dealer Code", wrapping : true}),

  template: new sap.ui.commons.TextField({

  id:"dlrNum",

  value: "{DealerNo}" ,

  maxLength: 10,

  liveChange: function(oEvent) {

 

                    var val = this.getValue();

                    sap.ui.getCore().byId("dlrNum").attachBrowserEvent("keypress",oController.acceptOnlyNumbers(val));

 

  },

in controller.js, i have defined the function:

  acceptOnlyNumbers: function(oEvent) {

    var key_codes = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 0, 8];

    if (!($.inArray(oEvent.which, key_codes) >= 0)) {

      oEvent.preventDefault();

    }

  },

This may not be the correct way of doing it, but then can you tell me how do i do it??

Thank you,

Regards,

Chetna

Former Member
0 Kudos

newDvrTable.addColumn(new sap.ui.table.Column({

  label :  new sap.ui.commons.Label({text: " Dealer Code", wrapping : true}),

  template : (new sap.ui.commons.TextField({  id:"dlrNum",

  value: "{DealerNo}" ,

  maxLength: 10,}).attachBrowserEvent("keypress",function(e){ 

   var key_codes = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 0, 8]; 

             if (!($.inArray(e.which, key_codes) >= 0)) { 

               e.preventDefault(); 

             }       

   }) ),

  }));

try this.

Dont use LiveChange event

former_member197578
Participant
0 Kudos

Thank you,

Best Regards

former_member182372
Active Contributor
0 Kudos

are you sure the error is on that line?