Jump to content

Recommended Posts

i have taken this code and see that it is possible to fool the script in to thinking that more than one word was typed by entering many spaces.  How do I count actual words, or thinking about it how to i trim the spaces from the start and end of the string before it is checked ?

 

http://www.mediacollege.com/internet/javascript/text/count-words.html

 

 

if (document.getElementsByName('fullname')[0].value.split(' ').length < 2) {
	// do something
	}

It's been a while, but something like this... It also handles extra white space between words too!

String.prototype.trim = function () {
    //return this.replace(/^\s*/, "").replace(/\s*$/, "");
    return this.replace(/^\s+|\s+$/g, '') ;
}

var ss = "   I told   you it'd   be   alright!   ".trim().replace(/\s+/g,' ');
alert("ss: " + ss);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.