Avatar billede guzzie Nybegynder
16. august 2001 - 14:19 Der er 6 kommentarer og
1 løsning

Help with a form function

Hi There

I am trying to get my form to calculate a price for me
in my db the prices are in DK ie: 56,50
The form can calculate the price if it is a whole number like 43,00 <--  however when the price is like this 43,50 then the price does not get calculated ?

heres some of the code I use to work out the price
I dont know if this is an ASP error or javascript

<script language=\"JavaScript\">
function Wine(Pris)
{
  string = \"\" + Pris;
  number = string.length - string.indexOf(\',\');
  if (string.indexOf(\',\') == -1)
      return string + \',00\';
  if (number == 1)
      return string + \'00\';
  if (number == 2)
      return string + \'0\';
  if (number > 3)
      return string.substring(0,string.length-number+3);
return string;
}

function calculate()
{
document.multi.result1.value = Wine((document.multi.Artikel1.value)*(document.multi.Antal1.value))
}

and the input type looks like this

<input type=\"hidden\" value=\"<%=wine_info(\"Pris\")%>0\" name=\"Artikel1\" size=\"1\" onChange=\"calculate()\">

Anybody got a good idea ? or is there an easier way to do it
Thanks Guzzie
Avatar billede teepee Nybegynder
16. august 2001 - 15:10 #1
Are you sure that your returned value of
((document.multi.Artikel1.value)*(document.multi.Antal1.value)) is a NaN or Not if any comma exists, try using . as comma instead. And when you converts to string, then look for the . instead of ,
Avatar billede guzzie Nybegynder
16. august 2001 - 15:52 #2
Hey There teepee

I cant really work it out
heres the javascript again :=)
<script language=\"JavaScript\">
function Wine(Pris)
{
  string = \"\" + Pris;
  number = string.length - string.indexOf(\',\');
  if (string.indexOf(\'.\') == -1)
      return string + \',00\';
  if (number == 1)
      return string + \'00\';
  if (number == 2)
      return string + \'0\';
  if (number > 3)
      return string.substring(0,string.length-number+3);
return string;
}

function calculate()
{
document.multi.result1.value = Wine((document.multi.Artikel1.value)*(document.multi.Antal1.value))
}
</script>

and here are the 3 areas from the form
the first is the price from my db
<input type=\"hidden\" value=\"<%=wine_info(\"Pris\")%>\" name=\"Artikel1\" size=\"1\" onChange=\"calculate()\">
the next is how many time it gets calculated
<SELECT ID=FORM name=\"Antal1\" size=\"1\" onChange=\"calculate()\">

And heres where the value get inserted
<INPUT type=text name=\"result1\" size=\"8\" value=\"0.00\" onfocus=this.blur() >

When I do choose a value like 45,50 then it gets displayed as NaN,00
you named it before(NaN) <-- but I dont know what NaN is :(
Any ideas
Guzzie
Avatar billede teepee Nybegynder
16. august 2001 - 16:48 #3
NaN - not a number
It cannot time 45,50 with the number of items, for instance 3*45,50 => is not a number, however 45.50*3 => is a number! I\'m not sure whether regional settings can solve this problem, otherwise use a (.) instead of (,)
Avatar billede teepee Nybegynder
16. august 2001 - 16:48 #4
Or replace at runtime before calculating
Avatar billede guzzie Nybegynder
16. august 2001 - 17:44 #5
hi there teepee

how would I go about replacing at runtime ?
or do you have a link to where there r examples ?
I think this would be a good idea as the database is already set up to accept DK
Guzzie
Avatar billede guzzie Nybegynder
16. august 2001 - 18:35 #6
no problem teepee I got the function working
thanks for the advice
Weeeee
Guzzie
Avatar billede guzzie Nybegynder
16. august 2001 - 18:36 #7
this works great
<%
FUNCTION fixComma( thisString )
    fixComma = Replace( thisString, \",\", \".\" )
END FUNCTION
%>
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester