on 04-05-2015 4:40 PM
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
Thank you all for responding.
Finally worked,
Best Regards,
Chetna
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Copy pasted the same code . It works!!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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
are you sure the error is on that line?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
84 | |
10 | |
10 | |
9 | |
7 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.