White Space Problem with JavaScript

This tutorial will explain about how to solve white space problem with JavaScript. One time, I founded bug on my PHP script. White space was broke it! Shit!. However javascript doesn’t have trim function like PHP. For example, if you want to validate user form using jQuery and PHP, my advice is: you must check the white spaces before send string to PHP Action Script. Then you will fine to use PHP trim function or class defined.
It’s fuck’n crazy. My jQuery not respond If I don’t check white space before send string into PHP script. So I need to find the white space before send it!
Here are my simple javascript code to find and count the white spaces:

// Count the number of times a substring is in a string.
String.prototype.substrCount =
  function(s) {
    return this.length && s ? (this.split(s)).length - 1 : 0;
  };
//some function to implementad
function loadContentResult(id) {
	if(id.substrCount(' ') > 0)
	{
		window.alert('I found white spaces on field');
	}
       else
       {
		window.alert('White spaces cleared!! You may add the next PHP / jQuery here');
       }

First: counting white space as problem caused

String.prototype.substrCount =
  function(s) {
    return this.length && s ? (this.split(s)).length - 1 : 0;
  };

Next, we will try the scripts as user id validator on my jQuery tutorial. See you!

2 thoughts on “White Space Problem with JavaScript”

Leave a Reply

Your email address will not be published. Required fields are marked *