Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
193
rated 0 times [  198] [ 5]  / answers: 1 / hits: 20070  / 12 Years ago, wed, november 28, 2012, 12:00:00

I'm using AJAX to retrieve data from MYSQL database through PHP.



However, if there is no result found, the variable still has two spaces. I found the problem using alert(data.length);. The result is 2, which means there are two spaces.



How can I remove these spaces so that if there is no result, I could display a message using if(data == ''){}?



Thank you!


More From » javascript

 Answers
7

This is called string trimming. And here is one option for that in pure JavaScript:



var len = data.replace(/s/g, ).length;


However, in modern browsers there is a string trim() function for that:



var len = data.trim().length;

[#81738] Tuesday, November 27, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
myakylas

Total Points: 66
Total Questions: 85
Total Answers: 95

Location: Guadeloupe
Member since Sat, Aug 22, 2020
4 Years ago
myakylas questions
Thu, Apr 28, 22, 00:00, 2 Years ago
Thu, Apr 8, 21, 00:00, 3 Years ago
Sat, Sep 19, 20, 00:00, 4 Years ago
;