...

View Full Version : avoid entering space



rama
07-08-2002, 07:37 AM
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

A1ien51
07-08-2002, 07:57 AM
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>

A1ien51
07-08-2002, 04:48 PM
I typed it up in another format if you want to see it work:

http://www10.brinkster.com/a1ien51/Scripts/RemoveSpaces.htm

A1ien51

rama
07-09-2002, 05:44 AM
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

glenngv
07-09-2002, 06:34 AM
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.

adios
07-10-2002, 03:20 AM
http://www.codingforums.com/showthread.php?threadid=1703 :thumbsup:

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

rama
07-10-2002, 05:01 PM
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

hryan
07-11-2002, 07:41 AM
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
:)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum