Hi following code throws the "Object doesn't support this property or method" error in IE..
var value = $input.val().replace(/ /g, '').trim();
It is part of form validation, in every other browser is it working well, but in IE it doesnt..please help
2 Answers
As was commented on your question, IE8 doesn't support trim(). jQuery includes the trim() function, so I'd use that:
var value = jQuery.trim($input.val().replace(/ /g, ''));
1 Comment
simekadam
Sorry, I can only accept only one answer:)But thanks, I'd read the api and not mindless autowrite the code:DI thought I was using trim form jQuery..Everything clear already
From Ben Rowe: Add the following code to add trim functionality to the string.
if(typeof String.prototype.trim !== 'function') {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, '');
}
}
trim()was introduced with ECMAScript 5 and IE8 does not support this. Actually it does not seem you needtrimeanyway, because you already remove every space by callingreplace(/ /g, ''). If you are worried about the other whitespace characters, you could use\s:$input.val().replace(/\s+/g, '').