Snuur
Snuur

Reputation: 309

function works but gives errors

The following function works perfectly. However it gives errors in console, and prevents jqueryui from working.

function removenonbtwchars(input){

    input = input.toUpperCase();
    input = input.replace(/\W/g, '');
    input = input.substring(0,16);

    return input;
}

error:

Uncaught TypeError: Cannot call method 'toUpperCase' of undefined scripts_V2.js:1924
removenonbtwchars scripts_V2.js:1924
(anonymous function) scripts_V2.js:1934
fire jquery-1.9.1.js:1037
self.fireWith jquery-1.9.1.js:1148
jQuery.extend.ready jquery-1.9.1.js:433
completed

Anyone got an idea?

Upvotes: 1

Views: 272

Answers (3)

benzford
benzford

Reputation: 111

I met almost the same error, finally I found that

I didn't insert "()" between "}" and ");" where in the end of the definition of the object which own the method .

Upvotes: 0

zzlalani
zzlalani

Reputation: 24354

You need to call this function with some string in it.

var input = removenonbtwchars("some thing");

if you are calling it with some variable try this

var input = '';
if ( $.trim(data) != '' ) {
    input = removenonbtwchars(data);
}

but remember that the variable data should be string because toUpperCase() is a method of string class

Upvotes: 1

jgillich
jgillich

Reputation: 76199

Shorter versions that returns an empty string when you pass a falsy value (like undefined):

function removenonbtwchars(input){
    return input ? input.toUpperCase().replace(/\W/g, '').substring(0,16) : "";
}

Upvotes: 0

Related Questions