Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > pls help me run this code

Reply
Thread Tools

pls help me run this code

 
 
Manu Ashok
Guest
Posts: n/a
 
      04-06-2004
can any body help me with this...........
i donno what's happening.....the value enterd is not being
trimmed.....it leaves the blank spaces as it is in the form.


<SCRIPT language=javascript>

function trim(inputString) {
if (typeof inputString != "string") {
return inputString;
}
var retValue = inputString;
var ch = retValue.substring(0, 1);
while (ch == " ") {
retValue = retValue.substring(1, retValue.length);
ch = retValue.substring(0, 1);
}
ch = retValue.substring(retValue.length-1, retValue.length);
while (ch == " ") {
retValue = retValue.substring(0, retValue.length-1);
ch = retValue.substring(retValue.length-1, retValue.length);
}
while (retValue.indexOf(" ") != -1) {
retValue = retValue.substring(0, retValue.indexOf(" ")) +
retValue.substring(retValue.indexOf(" ") + 1, retValue.length);
}
return retValue;
}

function checkint(txtObj) {
var retValue;
trim(txtObj);
var numbervalue = txtObj;
for(var i = 0; i < numbervalue.length; ++i){
tmptegn = numbervalue.charAt(i);
if (tmptegn < "0" || tmptegn > "9"){
if(tmptegn == " ")
i++;
else
{ alert("only numeric value accepted")
txtObj.focus();
txtObj.select();
return;
}
}
return true;
}
}



in the form i call it as follows

<td><input type=text name="CategoryName" value="<%= CategoryName %>"
maxlength=250 onBlur="trim(this)"> </td>

<td><input type=text name="CategoryCode" value="<%= CategoryCode %>"
maxlength=50 OnBlur="checkint(this)"> </td>
 
Reply With Quote
 
 
 
 
Brian Genisio
Guest
Posts: n/a
 
      04-06-2004
Manu Ashok wrote:

> can any body help me with this...........
> i donno what's happening.....the value enterd is not being
> trimmed.....it leaves the blank spaces as it is in the form.
>
>
> <SCRIPT language=javascript>
>
> function trim(inputString) {
> if (typeof inputString != "string") {
> return inputString;
> }
> var retValue = inputString;
> var ch = retValue.substring(0, 1);
> while (ch == " ") {
> retValue = retValue.substring(1, retValue.length);
> ch = retValue.substring(0, 1);
> }
> ch = retValue.substring(retValue.length-1, retValue.length);
> while (ch == " ") {
> retValue = retValue.substring(0, retValue.length-1);
> ch = retValue.substring(retValue.length-1, retValue.length);
> }
> while (retValue.indexOf(" ") != -1) {
> retValue = retValue.substring(0, retValue.indexOf(" ")) +
> retValue.substring(retValue.indexOf(" ") + 1, retValue.length);
> }
> return retValue;
> }
>
> function checkint(txtObj) {
> var retValue;
> trim(txtObj);
> var numbervalue = txtObj;
> for(var i = 0; i < numbervalue.length; ++i){
> tmptegn = numbervalue.charAt(i);
> if (tmptegn < "0" || tmptegn > "9"){
> if(tmptegn == " ")
> i++;
> else
> { alert("only numeric value accepted")
> txtObj.focus();
> txtObj.select();
> return;
> }
> }
> return true;
> }
> }
>
>
>
> in the form i call it as follows
>
> <td><input type=text name="CategoryName" value="<%= CategoryName %>"
> maxlength=250 onBlur="trim(this)"> </td>
>
> <td><input type=text name="CategoryCode" value="<%= CategoryCode %>"
> maxlength=50 OnBlur="checkint(this)"> </td>


Way too complicated... Try this... It will trim tabs and spaces from
both ends.

function trimLeft(s) { return s.replace(/^[\t ]+/, ""); }
function trimRight(s) { return s.replace(/[\t ]+$/, ""); }
function trim(s) { return trimLeft(trimRight(s)); }

Brian

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
URGENT - Pls help...pls recommend - laptop purchase irfansmith@gmail.com Computer Information 2 08-15-2008 11:34 PM
Pls help me to validate some code! jollyroger HTML 4 08-06-2007 01:32 PM
pls help me when i sent mail, it vil sending twice instead of once ,am using java.mail,am sending my code.... shailajabtech@gmail.com Java 0 09-28-2006 06:38 AM
pls, help.. i need a number..pls olabanji timothy MCSE 7 09-10-2003 04:02 PM
I am a newbie ! Pls help! VS.net direct x 9 sample problem Unable to run supo C++ 1 07-21-2003 07:20 PM



Advertisments