...

View Full Version : wierd problem (please help)



eranwein
01-28-2006, 08:26 AM
i have a text field which gets it's value via php variable that is intialized from mysql database. the user is suppose to be able to change the value in the text field and by pressing "update" button the function update price is called.

there i'm checking first if the value in the text filed is float. and here is the problem. if i change the value in the text field to a valif float number, it works fine. but if i press update with the original value in the text field, it doesnt recognize it as float, althogh the value in the database is float.
even if i delete the original value and then write the same original value again then it works fine.

in the code below you can see that the alert function prints the value of the text field. there i could see that even thow the original value was endeed a float, it didnt recxognize it as such, but again - when i've delete it and write the same original value myself, it did recognize it as float.


<script>

function is_float(str)
{
return /^[0-9]+\.[0-9]+$/.test(str) || /^[0-9]+$/.test(str);
}

function updatePrice()
{
frm=document.forms['frmDailyUpdate'];
var newPrice = frm['itemPriceField'].value;
if (is_float(newPrice))
{
alert("valid price:" + newPrice);
}
else
{
alert("please insert a valid price:" + newPrice);
}
}

</script>

<form name="frmDailyUpdate" method="post" action="daily_update.php?>
<input name="itemPriceField" type="text" value="<? print "$currentItemPrice" ?
<input type = "button" name = "b" onclick="updatePrice()"/>> />

function is_float(str)
{
return /^[0-9]+\.[0-9]+$/.test(str) || /^[0-9]+$/.test(str);
}

function updatePrice()
{
frm=document.forms['frmDailyUpdate'];
var newPrice = frm['itemPriceField'].value;
if (is_float(newPrice))
{
alert("valid price:" + newPrice);
}
else
{
alert("please insert a valid price:" + newPrice);
}
}

subhailc
01-28-2006, 08:41 AM
parseFloat?

eranwein
01-28-2006, 09:01 AM
parseFloat will not solve my problem because it's a free to write string.
what if the user prints "something" in the text field?
in need to force him to write a valid float number because it's a price..

funhunter
01-28-2006, 09:36 AM
possible the value of $currentItemprice is the string value without a dot(.) ?
because if my user has registered with an id like "some_thing" but when I print this value again from Mysql it's always be "some thing" ,through if I do not print out,comparing id of user(some_thing) when he login with that value from mysql always return correct result?

eranwein
01-28-2006, 10:21 AM
1. i think that the value is not suppose to be a string because it is defined as float in the database.

2. even if it was a string, the function isFloat(str) refers to "str" as string (as you can see in the code i've attached).

the original price is 11.00.
when i press on the update button without touching the text field, the alert function prints: "please insert a valid price":11.00"
but, when i delete the 11.00, and then print it myself (11.00) and press on the update button again, the alert function prints: "valid price:11.00"

subhailc
01-28-2006, 11:18 AM
restrict it to numbers then, with something like

onkeydown="return isFinite(parseInt(String.fromCharCode(event.keyCode)))"

eranwein
01-28-2006, 11:30 AM
ok, that's a progress.
but how do i get a float string such as: 15.45?
is the only solution is to devide it into two seperate txtFields?

vwphillips
01-28-2006, 11:36 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title></title>
<script language="JavaScript" type="text/javascript">
<!--

function CkFloat(id){
var val=document.getElementById(id).value;
if (isNaN(val)||val.length<1){ return; }
val=parseFloat(val)%1;
if (val!=0){
alert('Float');
}
else {
alert('Integer');

}
}
//-->
</script></head>

<body>
<input id="TB" >
<input type="button" name="" value="Check for float" onclick="CkFloat('TB');" >
</body>

</html>

eranwein
01-28-2006, 12:38 PM
thank you very much.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum