Saturday, June 21, 2008

LTrim, RTrim, IsNumeric function in Javascript

Javascript are supported by all the browser so some function are there which is replacement of VB Script as VB Script support only by IE and not others browsers so some problems in using VB Script.

One of the function is LTRIM. This function is used to remove spaces from the left of the string. this function accept one string and remove all spaces from left side and return string.

Here is the defination :-

function LTrim( value ) {

var re = /\s*((\S+\s*)*)/;
return value.replace(re, "$1");

}

Rtrim function is used to remove ending spaces through javascript.
Here is coding :-

// Removes ending whitespaces
function RTrim( value )
{
var re = /((\s*\S+)*)\s*/;
return value.replace(re, "$1");
}


isNumeric function is used to check string is numeric or not.
here is this code.

function IsNumeric(strString)
// check for valid numeric strings
{
var strValidChars = "0123456789.";
var strChar;
var blnResult = true;

if (strString.length == 0) return false;

// test strString consists of valid characters listed above
for (i = 0; i < strString.length && blnResult == true; i++)
{
strChar = strString.charAt(i);

if (strValidChars.indexOf(strChar) == -1)
{
blnResult = false;
}
if(strChar==".")
{
strValidChars = "0123456789";
}
}
return blnResult;
}

No comments: