Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Jan 2006
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    wierd problem (please help)

    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);
    }
    }

  • #2
    Regular Coder
    Join Date
    Jan 2004
    Posts
    185
    Thanks
    2
    Thanked 1 Time in 1 Post
    parseFloat?

  • #3
    New Coder
    Join Date
    Jan 2006
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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..

  • #4
    New Coder
    Join Date
    Jan 2006
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #5
    New Coder
    Join Date
    Jan 2006
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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"

  • #6
    Regular Coder
    Join Date
    Jan 2004
    Posts
    185
    Thanks
    2
    Thanked 1 Time in 1 Post
    restrict it to numbers then, with something like
    Code:
    onkeydown="return isFinite(parseInt(String.fromCharCode(event.keyCode)))"

  • #7
    New Coder
    Join Date
    Jan 2006
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #8
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,521
    Thanks
    3
    Thanked 507 Times in 494 Posts
    Code:
    <!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>

  • #9
    New Coder
    Join Date
    Jan 2006
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you very much.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •