Web Analytics Made Easy -
StatCounter Javascript white space - CodingForum

Announcement

Collapse
No announcement yet.

Javascript white space

Collapse
X
 
  • 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

    Comment


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

      Code:
      string = string.replace(/\s+/, ' ');
      Stephen
      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.

      Comment

      Working...
      X