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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Aug 2006
    Posts
    105
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Expert Javascripters help needed on validation short script

    So I have this web page on my intranet, it was made by my predecessor.
    there's 3 fields.

    field1 - Qty needed
    field2 - Qty to order
    field3 - Qty in stock

    <input type='text' maxlength='8' name='quantity1' size='20' value='".htmlspecialchars($quot->quantity1, ENT_QUOTES)."' ".$this->readonly." onKeyUp=\"validateNumeric(this);calculateStock(quantity1, quantity2, quantity3);\" onChange=\"calculateStock(quantity1, quantity2, quantity3);\" >


    <input type='text' maxlength='8' name='quantity2' size='20' value='".htmlspecialchars($quot->quantity2, ENT_QUOTES)."' ".$this->readonly." onKeyUp=\"validateNumeric(this);calculateStock(quantity1, quantity2, quantity3);\" onChange=\"calculateStock(quantity1, quantity2, quantity3);\">

    <td><input type='text' maxlength='8' name='quantity3' size='20' value='".htmlspecialchars($quot->quantity3, ENT_QUOTES)."' readonly >


    You don't need to worry about the value thing, that's PHP values since the javascript is in a php variable. but it's the javascript doing funny business.

    Here's the function:

    function calculateStock( field1, field2, field3 ){
    if( field1.value >= field2.value){
    field3.value = field1.value - field2.value;
    }
    if( field2.value - field1.value > 0 ){
    field2.value = field1.value;
    alert(\"".$this->lang['valueNotValid']."\");
    field3.value = 0;
    }


    The problem is very hard to describe, because sometimes it works sometimes it doesn't. Sometimes it doesn't because i don't fully understand how to reproduce it actually.

    Basically field1 is what first enter
    then you enter field two, the difference is field3
    it all happens real-time kinda thing

    I know i'm being vague, but whenever I have a 0 after a digit in field2, it doesn't work properly. It's almost as if 60340, it thinks after the first 0 it's the end and it's only 60.

    Can anyone help?

  • #2
    New Coder
    Join Date
    Nov 2006
    Posts
    52
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Could you post the contents of the validateNumeric() function?

  • #3
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Javascript compares as text for fields like that.
    You have to parseInt / parseFloat for form fields being compared with the math operators < and > etc

    i.e.
    parseFloat(field1.value)

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/


  •  

    Posting Permissions

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