Web Analytics Made Easy -
StatCounter avoid entering space - CodingForum

Announcement

Collapse
No announcement yet.

avoid entering space

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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>
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

    Comment


    • #3
      I typed it up in another format if you want to see it work:

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

      A1ien51
      Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

      Comment


      • #4
        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

        Comment


        • #5
          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.
          Glenn
          vBulletin Mods That Rock!

          Comment


          • #6
            http://www.codingforum.net/showthread.php?threadid=1703

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

            Comment


            • #7
              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

              Comment


              • #8
                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

                Comment

                Working...
                X