Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Persistent Space Character in an Input Text Field

Reply
Thread Tools

Persistent Space Character in an Input Text Field

 
 
kvnsmnsn@hotmail.com
Guest
Posts: n/a
 
      02-27-2007
I've written the following Javascript file that includes an input text
field and an output text field, the latter of which is initialized to
zero. Each time the user enters a number in the input field and hits
the space bar, that number gets added to the value in the output field
and then the input field gets set to the empty string. The user can
enter as many numbers into the input field as s/he wants.

The problem I have with this is that even though I do set "sbInput" to
the empty string, somehow a space character ends up in it, so that if
I just type the number "123" into it, what actually ends up is " 123".
This doesn't affect the correct running of the web page, since a space
character doesn't change the numeric value of that field, but it's a
nuisance that I'd like to get rid of. Does anybody know how I can get
rid of this extra space character printing? Any information on this
would be appreciated.

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-
transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Space Bug</title>
<script language="javascript">

function keyCheck ( evnt)
{
var theKey;
var sbIn = document.getElementById( "sbInput");
var sbOut = document.getElementById( "sbOutput");
if (!evnt)
{ evnt = window.event;
}
if (evnt.keyCode)
{ theKey = evnt.keyCode;
}
else if (evnt.which)
{ theKey = evnt.which
}
if (theKey == 32)
{ if (evnt.keyCode)
{ evnt.cancelBubble = true;
}
sbOut.value = Number( sbOut.value) + Number( sbIn.value);
sbIn.value = "";
}
}

</script>
</head>
<body>
<form name="sbForm">
<input type="text" name="sbInput" id="sbInput" size="5"
onkeypress="keyCheck( event);" />
<input type="text" name="sbOutput" id="sbOutput" size="5"
readonly="readonly" value="0" />
</form>
</body>
</html>

 
Reply With Quote
 
 
 
 
Tom Cole
Guest
Posts: n/a
 
      02-27-2007
On Feb 27, 1:38 pm, (E-Mail Removed) wrote:
> I've written the following Javascript file that includes an input text
> field and an output text field, the latter of which is initialized to
> zero. Each time the user enters a number in the input field and hits
> the space bar, that number gets added to the value in the output field
> and then the input field gets set to the empty string. The user can
> enter as many numbers into the input field as s/he wants.
>
> The problem I have with this is that even though I do set "sbInput" to
> the empty string, somehow a space character ends up in it, so that if
> I just type the number "123" into it, what actually ends up is " 123".
> This doesn't affect the correct running of the web page, since a space
> character doesn't change the numeric value of that field, but it's a
> nuisance that I'd like to get rid of. Does anybody know how I can get
> rid of this extra space character printing? Any information on this
> would be appreciated.
>
> ---Kevin Simonson
>
> "You'll never get to heaven, or even to LA,
> if you don't believe there's a way."
> from _Why Not_
>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-
> transitional.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml">
> <head>
> <title>Space Bug</title>
> <script language="javascript">
>
> function keyCheck ( evnt)
> {
> var theKey;
> var sbIn = document.getElementById( "sbInput");
> var sbOut = document.getElementById( "sbOutput");
> if (!evnt)
> { evnt = window.event;
> }
> if (evnt.keyCode)
> { theKey = evnt.keyCode;
> }
> else if (evnt.which)
> { theKey = evnt.which
> }
> if (theKey == 32)
> { if (evnt.keyCode)
> { evnt.cancelBubble = true;
> }
> sbOut.value = Number( sbOut.value) + Number( sbIn.value);
> sbIn.value = "";
> }
> }
>
> </script>
> </head>
> <body>
> <form name="sbForm">
> <input type="text" name="sbInput" id="sbInput" size="5"
> onkeypress="keyCheck( event);" />
> <input type="text" name="sbOutput" id="sbOutput" size="5"
> readonly="readonly" value="0" />
> </form>
> </body>
> </html>


Try something like this. Have your method return false if a space is
detected or true otherwise:

if (theKey == 32)
> { if (evnt.keyCode)
> { evnt.cancelBubble = true;
> }
> sbOut.value = Number( sbOut.value) + Number( sbIn.value);
> sbIn.value = "";

return false;
> }

return true;


Then change your event handler to:

<input type="text" name="sbInput" id="sbInput" size="5"
> onkeypress="return keyCheck( event);" />


That should do it.

 
Reply With Quote
 
 
 
 
shimmyshack
Guest
Posts: n/a
 
      02-27-2007
On 27 Feb, 19:03, "Tom Cole" <(E-Mail Removed)> wrote:
> On Feb 27, 1:38 pm, (E-Mail Removed) wrote:
>
>
>
> > I've written the following Javascript file that includes an input text
> > field and an output text field, the latter of which is initialized to
> > zero. Each time the user enters a number in the input field and hits
> > the space bar, that number gets added to the value in the output field
> > and then the input field gets set to the empty string. The user can
> > enter as many numbers into the input field as s/he wants.

>
> > The problem I have with this is that even though I do set "sbInput" to
> > the empty string, somehow a space character ends up in it, so that if
> > I just type the number "123" into it, what actually ends up is " 123".
> > This doesn't affect the correct running of the web page, since a space
> > character doesn't change the numeric value of that field, but it's a
> > nuisance that I'd like to get rid of. Does anybody know how I can get
> > rid of this extra space character printing? Any information on this
> > would be appreciated.

>
> > ---Kevin Simonson

>
> > "You'll never get to heaven, or even to LA,
> > if you don't believe there's a way."
> > from _Why Not_

>
> > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> > "http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-
> > transitional.dtd">
> > <html xmlns="http://www.w3.org/1999/xhtml">
> > <head>
> > <title>Space Bug</title>
> > <script language="javascript">

>
> > function keyCheck ( evnt)
> > {
> > var theKey;
> > var sbIn = document.getElementById( "sbInput");
> > var sbOut = document.getElementById( "sbOutput");
> > if (!evnt)
> > { evnt = window.event;
> > }
> > if (evnt.keyCode)
> > { theKey = evnt.keyCode;
> > }
> > else if (evnt.which)
> > { theKey = evnt.which
> > }
> > if (theKey == 32)
> > { if (evnt.keyCode)
> > { evnt.cancelBubble = true;
> > }
> > sbOut.value = Number( sbOut.value) + Number( sbIn.value);
> > sbIn.value = "";
> > }
> > }

>
> > </script>
> > </head>
> > <body>
> > <form name="sbForm">
> > <input type="text" name="sbInput" id="sbInput" size="5"
> > onkeypress="keyCheck( event);" />
> > <input type="text" name="sbOutput" id="sbOutput" size="5"
> > readonly="readonly" value="0" />
> > </form>
> > </body>
> > </html>

>
> Try something like this. Have your method return false if a space is
> detected or true otherwise:
>
> if (theKey == 32)> { if (evnt.keyCode)
> > { evnt.cancelBubble = true;
> > }
> > sbOut.value = Number( sbOut.value) + Number( sbIn.value);
> > sbIn.value = "";

>
> return false;> }
>
> return true;
>
> Then change your event handler to:
>
> <input type="text" name="sbInput" id="sbInput" size="5"
>
> > onkeypress="return keyCheck( event);" />

>
> That should do it.


onkeypress, the character gets input after function call; if you use
onkeyup, it shouldn't, did u decide against onkeyup?

 
Reply With Quote
 
kvnsmnsn@hotmail.com
Guest
Posts: n/a
 
      02-27-2007
On Feb 27, 12:13 pm, "shimmyshack" <(E-Mail Removed)> wrote:
> On 27 Feb, 19:03, "Tom Cole" <(E-Mail Removed)> wrote:
>
>
>
> > On Feb 27, 1:38 pm, (E-Mail Removed) wrote:

>
> > > I've written the following Javascript file that includes an input text
> > > field and an output text field, the latter of which is initialized to
> > > zero. Each time the user enters a number in the input field and hits
> > > the space bar, that number gets added to the value in the output field
> > > and then the input field gets set to the empty string. The user can
> > > enter as many numbers into the input field as s/he wants.

>
> > > The problem I have with this is that even though I do set "sbInput" to
> > > the empty string, somehow a space character ends up in it, so that if
> > > I just type the number "123" into it, what actually ends up is " 123".
> > > This doesn't affect the correct running of the web page, since a space
> > > character doesn't change the numeric value of that field, but it's a
> > > nuisance that I'd like to get rid of. Does anybody know how I can get
> > > rid of this extra space character printing? Any information on this
> > > would be appreciated.

>
> > > ---Kevin Simonson

>
> > > "You'll never get to heaven, or even to LA,
> > > if you don't believe there's a way."
> > > from _Why Not_

>
> > > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> > > "http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-
> > > transitional.dtd">
> > > <html xmlns="http://www.w3.org/1999/xhtml">
> > > <head>
> > > <title>Space Bug</title>
> > > <script language="javascript">

>
> > > function keyCheck ( evnt)
> > > {
> > > var theKey;
> > > var sbIn = document.getElementById( "sbInput");
> > > var sbOut = document.getElementById( "sbOutput");
> > > if (!evnt)
> > > { evnt = window.event;
> > > }
> > > if (evnt.keyCode)
> > > { theKey = evnt.keyCode;
> > > }
> > > else if (evnt.which)
> > > { theKey = evnt.which
> > > }
> > > if (theKey == 32)
> > > { if (evnt.keyCode)
> > > { evnt.cancelBubble = true;
> > > }
> > > sbOut.value = Number( sbOut.value) + Number( sbIn.value);
> > > sbIn.value = "";
> > > }
> > > }

>
> > > </script>
> > > </head>
> > > <body>
> > > <form name="sbForm">
> > > <input type="text" name="sbInput" id="sbInput" size="5"
> > > onkeypress="keyCheck( event);" />
> > > <input type="text" name="sbOutput" id="sbOutput" size="5"
> > > readonly="readonly" value="0" />
> > > </form>
> > > </body>
> > > </html>

>
> > Try something like this. Have your method return false if a space is
> > detected or true otherwise:

>
> > if (theKey == 32)> { if (evnt.keyCode)
> > > { evnt.cancelBubble = true;
> > > }
> > > sbOut.value = Number( sbOut.value) + Number( sbIn.value);
> > > sbIn.value = "";

>
> > return false;> }

>
> > return true;

>
> > Then change your event handler to:

>
> > <input type="text" name="sbInput" id="sbInput" size="5"

>
> > > onkeypress="return keyCheck( event);" />

>
> > That should do it.

>
> onkeypress, the character gets input after function call; if you use
> onkeyup, it shouldn't, did u decide against onkeyup?


To be completely honest I was not aware that "onkeyup" existed. I
tried it and it solved the problem completely. Thanks!

 
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
Persistent field and Persistent properties - difference gk Java 7 10-12-2010 09:43 PM
1.Enter space bar for field names and save the field.The field shoud not get saved and an alert should be there as"Space bars are not allowed" Sound Javascript 2 09-28-2006 02:43 PM
Why Python style guide (PEP-8) says 4 space indents instead of 8 space??? 8 space indents ever ok?? Christian Seberino Python 21 10-27-2003 04:20 PM
Re: Why Python style guide (PEP-8) says 4 space indents instead of8 space??? 8 space indents ever ok?? Ian Bicking Python 2 10-23-2003 07:07 AM
Stack space, global space, heap space Shuo Xiang C Programming 10 07-11-2003 07:30 PM



Advertisments