pls help me run this code

Discussion in 'Javascript' started by Manu Ashok, Apr 6, 2004.

  1. Manu Ashok

    Manu Ashok Guest

    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>
     
    Manu Ashok, Apr 6, 2004
    #1
    1. Advertising

  2. 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
     
    Brian Genisio, Apr 6, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. supo
    Replies:
    1
    Views:
    395
    Aggro
    Jul 21, 2003
  2. jollyroger

    Pls help me to validate some code!

    jollyroger, Aug 6, 2007, in forum: HTML
    Replies:
    4
    Views:
    373
    Andy Dingley
    Aug 6, 2007
  3. Vasu
    Replies:
    3
    Views:
    342
    John Bode
    Sep 13, 2007
  4. Dave Ello

    Help pls - need to NS-proof some code!

    Dave Ello, Nov 28, 2003, in forum: Javascript
    Replies:
    2
    Views:
    112
    Dave Ello
    Nov 28, 2003
  5. Susanne West
    Replies:
    1
    Views:
    112
    smallpond
    Apr 21, 2008
Loading...

Share This Page