Testing for blank input...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Me

    #1

    Testing for blank input...

    I need to test for blank inputs and run a routine "if" the input is
    blank. This will be used internally on a LAN to calculate selling price
    if the "sell" input is blank and a margin is supplied, and to calculate
    a margin if the margin input is blank and the sell price is supplied.
    The "cost" is set and will be doled out by the CGI. I've tried checking
    for a zero variable length ".length" and couldn't get it to work. It
    always fires if you check for -1 even if there is something in the
    input, and it never fires if you set it any higher. I'm sure I'm just
    doing it wrong. Here's where I'm at:

    <html>
    <head>
    <SCRIPT LANGUAGE="JavaS cript">
    <!--HIDE
    function doMath( ) {

    // var one will be set by the CGI when in use

    var one = eval(document.t heForm.
    elements[0].value)
    var two = eval(document.t heForm.
    elements[1].value)
    var three = eval(document.t heForm.
    elements[2].value)


    // Run only if sell input is blank

    var margin = ((one / three) * 100)
    document.theFor m.elements[1].value = margin

    // Run only if sell input is blank

    var sell = (one / ((100 - two) / 100))
    document.theFor m.elements[2].value = sell

    }
    //STOP HIDING-->
    </SCRIPT>
    <BODY><FORM NAME="theForm">
    Cost <INPUT TYPE="text"> Margin <INPUT TYPE="text"> Sell <INPUT TYPE="text">
    <INPUT TYPE="button" VALUE="Calculat e"
    onClick="doMath ( )">
    </FORM>
    </body>
    </html>


    Suggestions appreciated. :-)
  • Me

    #2
    Re: Testing for blank input...

    Me wrote:[color=blue]
    > I need to test for blank inputs and run a routine "if" the input is
    > blank. This will be used internally on a LAN to calculate selling price
    > if the "sell" input is blank and a margin is supplied, and to calculate
    > a margin if the margin input is blank and the sell price is supplied.
    > The "cost" is set and will be doled out by the CGI. I've tried checking
    > for a zero variable length ".length" and couldn't get it to work. It
    > always fires if you check for -1 even if there is something in the
    > input, and it never fires if you set it any higher. I'm sure I'm just
    > doing it wrong. Here's where I'm at:[/color]

    Got it. Dumb syntax problem. Here's what worked.

    <html>
    <head>
    <SCRIPT LANGUAGE="JavaS cript">

    function doMath( ) {
    var one = eval(document.t heForm.elements[0].value)
    var two = eval(document.t heForm.elements[1].value)
    var three = eval(document.t heForm.elements[2].value)


    // Run only if margin input is blank

    if (document.theFo rm.elements[1].value.length== 0){
    var margin = (100 - ((one / three) * 100));
    document.theFor m.elements[1].value = margin
    }

    // Run only if sell input is blank

    if (document.theFo rm.elements[2].value.length== 0){
    var sell = (one / ((100 - two) / 100));
    document.theFor m.elements[2].value = sell
    }

    }

    </SCRIPT>
    </head>

    <BODY>

    <FORM NAME="theForm">
    Cost <INPUT TYPE="text"> Margin <INPUT TYPE="text"> Sell <INPUT TYPE="text">
    <INPUT TYPE="button" VALUE="Calculat e" onClick="doMath ( )">
    </FORM>

    </body>
    </html>

    Comment

    Working...