...

View Full Version : Is Trim() function available in JScript



krishnan_sn
01-14-2004, 10:59 AM
hi,

Is Trim() function available in JScript. the problem i am facing is if the user presses a space bar the code below will not work. so in order to eliminate the blank spaces i want to use the trim function or similar to that....if possible please give me the sample code..

if(f.txttitle.value == "")
{
alert("Enter the Title for the Notice Message");
f.txttitle.focus();
return false;
}


thanks & regards

Kor
01-14-2004, 11:09 AM
Try

if(f.txttitle.value == "" || f.txttitle.value == " ")

Vladdy
01-14-2004, 12:27 PM
yeah:
function trim(str)
{ return str.replace(/^\s+/,'').replace(/\s+$/,'');
}

Choopernickel
01-14-2004, 02:31 PM
Prototypes, people, prototypes:

String.prototype.lTrim = function () {
var whitespace = new RegExp("^\\s+","gm");
return this.replace(whitespace, "");
}

String.prototype.rTrim = function () {
var whitespace = new RegExp("\\s+$","gm");
return this.replace(whitespace, "");
}

String.prototype.trim = function () {
return this.rTrim().lTrim();
}

Then you can use it as a method of any string object:

var myString = " Hello World ";
mySecondString = myString.rTrim(); // yields " Hello World"
myThirdString = myString.lTrim(); // yields "Hello World "
myFourthString = myString.trim(); // yields "Hello World"

See how nice and simple that is?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum