Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can you change a textbox's format?

Status
Not open for further replies.

Cineno

Programmer
Jul 24, 2006
142
US
Hello. I'm new with web design, and Frontpage in general.

Is there a way I can format a textbox so that when the user enters something it comes up as decimal format.

Like when the page loads it could say "0.0" in the box. Then the user clicks in it, and enters "18." After he clicks enter, or clicks out of the box, the format on the textbox changes it to "1.8" or ".18"

Is there a way to do this? Thanks for any help.
 
Something like this but this isn't a foolproof version. If the user enters 1.8 he will get 1..8
Code:
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<script language="vbscript">
sub numb_onChange
   dim val, vallen, lhs, rhs
   val = numb.value
   vallen = len(val)
   if vallen <> 0 then
      rhs = right(val,1)
      if vallen = 1 then
         lhs = ""
      else
         lhs = left (val, vallen - 1)
      end if
      numb.value = lhs & "." & rhs
   end if
end sub
</script>
</HEAD>
<BODY>

<P><INPUT type="text" name="numb"></P>
<INPUT type="text" name="dumb">
</BODY>
</HTML>
 
This is exactly what I needed thank you so much.
 
The only problem I see with it is that if the user goes back to enter more, it will insert another decimal place. So 1.8 will become 1.8.6 if they insert another 6.

I tried to enter another condition to see if there is already a decimal before the last number. However, this isn't working. The code's below. Got any ideas?

_______________________________________________
sub T5_onChange
dim val, vallen, lhs, rhs
val = T5.value
vallen = len(val)
if vallen <> 0 then
if left(right(T5.value,2),1) <> "." then
rhs = right(val,1)
if vallen = 1 then
lhs = 0
else
lhs = left (val, vallen - 1)
end if

T5.value = lhs & "." & rhs
end if
end if
end sub
 
Nevermind, I got it. I changed it to:

sub T5_onChange
dim val, vallen, lhs, rhs
'dim saveNum
val = T5.value
vallen = len(val)
if vallen <> 0 then
if InStr(val , ".") = 0 then '<-----Added condition
rhs = right(val,1)
if vallen = 1 then
lhs = 0
else
lhs = left (val, vallen - 1)
end if
'if T5.value <>
T5.value = lhs & "." & rhs
'T5.value = left(right(T5.value,2),1)
'saveNum = left(right(T5.value,2),1)
end if
end if
end sub

Thanks for the help.
 
I did say it wasn't a foolproof version. You have to test with all sorts of data. Here are some.
Code:
1800
1800.
180.0
18.00
1.800
fred
fried.rice
18..0
18.,0
The ones with at least one dot will bypass your test but are they correct? The real question is how idiot proof do you want it and how intelligent you think your users are. Kindergarten kids doing data entry on a stock exchange system can completely wreck it but somehow stock exchange workers hardly ever seem to.

Maybe a sanity check at the end would be useful. Something that takes cdbl of the value and does something with it to check that it is non zero. Nothing is idiot proof but do you expect idiots to use it?
 
Yeah I know idiots are going to use it. I put in more conditions since I pasted that, and tested it with everything I can imagine people putting in.

For my purposes, it's doing everything I want it to. Thanks for all your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top