Web Analytics Made Easy -
StatCounter Javascript white space - CodingForum


No announcement yet.

Javascript white space

  • Filter
  • Time
  • Show
Clear All
new posts

  • Javascript white space

    Hey guys !
    We all know what this code means " if(x !== ' ') " ,,,statement one
    else .......statement two

    If I input just one space in x variable then my code will go to the else branch,but if I input two white spaces (press space key two times) then will go on the first branch because my if statement check if there is only one white space.
    Is there any way I can make it in javascript to check a general white space like if my input form has no content just white spaces (like I press space key 10 times ) it will still go on the if branch and doesn t make else statement to happen?

  • #2
    PHP Code:
    string string.replace(/ +/, ' '); 
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


    • #3
      or the following to check for any whitespace characters (eg linefeeds) and not just spaces

      string = string.replace(/\s+/, ' ');
      Learn Modern JavaScript - http://javascriptexample.net/
      Helping others to solve their computer problem at http://www.felgall.com/

      Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.