Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jun 2002
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    avoid entering space

    Hi,

    I have a textbox in a form.I do not want the user to type space as first character of the textbox and last character .But he can enter space between two words.

    Only restriction is not allow space in the first and last character of the textbox

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    This script will remove all of the spaces in front of the text and behind the text. Checked only on IE.




    <html>
    <head>
    <script>
    function CheckIt(X,ABC){
    a=0;
    FL=ABC.slice(0,1);
    LL=ABC.slice(ABC.length-1,ABC.length);
    if(FL==" "){ABC=ABC.slice(1,ABC.length);a=1;}
    if(LL==" "){ABC=ABC.slice(0,ABC.length-1);a=1;}
    X.value=ABC;
    if(a!=0){CheckIt(X,ABC)}
    }
    </script>
    </head>
    <body>
    <form name="FormName">
    <input ytpe="text" onchange="CheckIt(this,this.value)" name="AName" size="12">
    </form>
    </body>
    </html>

  • #3
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    I typed it up in another format if you want to see it work:

    http://www10.brinkster.com/a1ien51/S...moveSpaces.htm

    A1ien51

  • #4
    New Coder
    Join Date
    Jun 2002
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    problem in IE

    hI,

    Your code works fine in NS but what happens in IE is that whe we enter space the first time ,followed by some characters and move focus to some other field ,the space is removed correctly ,but immediatly again if space is given as the first character and focus is moved to some other field onChange is not firing in IE and space is not getting removedimmediatly ,but working well with NS

    I am not able to detect the problem.Can u just look into it.

    Thanks
    Rama

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    String.prototype.trim=function() {
    var alt255_esc = "%A0";
    string = this;
    string_temp = string;
    slen = string_temp.length;
    //trim left of string
    for (i=0;i<slen;i++) {
    if ((string.charAt(i) == " ")||(escape(string.charAt(i)) == alt255_esc)) string_temp = string.substring(i+1,slen);
    else break;
    }
    string = string_temp;
    slen = string.length;
    //trim right of string
    for (i=slen-1;i>=0;i--) {
    if ((string.charAt(i) == " ")||(escape(string.charAt(i)) == alt255_esc)) string_temp = string.substring(0,i);
    else break;
    }
    return string_temp;
    }



    <input ytpe="text" onchange="this.value.trim()" name="AName" size="12">

    This code works for me and trims not only leading and trailing spaces but also alt+255's. But the code will be shorter if regular expression is used, which I'm not an expert at.

    If there is still inconsistencies in using onchange event in IE, try using onblur event instead.

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    how to trim a string

    <input onblur="this.value=this.value.trim()">

  • #7
    New Coder
    Join Date
    Jun 2002
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    problem with onChange event in IE5.0

    hi ,


    The following code for removing space works fine in Netscape ,but not in IE.For the first change it works in IE but immediatly if we change again,onChange event is not firing in IE

    Here is the code,



    <html>
    <head>
    <script>
    function CheckIt(X,ABC){
    a=0;
    FL=ABC.slice(0,1);
    LL=ABC.slice(ABC.length-1,ABC.length);
    if(FL==" "){ABC=ABC.slice(1,ABC.length);a=1;}
    if(LL==" "){ABC=ABC.slice(0,ABC.length-1);a=1;}
    X.value=ABC;
    if(a!=0){CheckIt(X,ABC)}
    }
    </script>
    </head>
    <body>
    <form name="FormName">
    <input ytpe="text" onchange="CheckIt(this,this.value)" name="AName" size="12">
    </form>
    </body>
    </html>


    It was given by Alien


    Thanks
    Rama

  • #8
    New Coder
    Join Date
    Jun 2002
    Location
    Bulgaria
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Rama!

    You can write a function checking the first and last symbol in your string:

    REPEAT THIS UNTIL CLEAR ALL BEGIN_SPACES
    var word=this.form.txtbox.value;
    if(word.charAt(0)==' ') word=word.substring(0,1);
    ----------------------------------------
    the same can do in end of string, after replaceing all spaces from begin of the string:

    REPEAT THIS UNTIL CLEAR ALL END_SPACES
    var end_pos=word.length-1;
    if(word.charAt(end_pos)==' ') word=word.substring(end_pos,1);
    ----------------------------------------
    It have to work in all browsers

    I didn't trace a code, only write on fly , so im may have some errors


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •