Need help with focus()

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

    Need help with focus()

    I cannot get my focus() fn to work in my validateInput(u serInput) fn. It
    will not allow the user to go back and correct invalid input like it should.
    Instead it goes right on through with the invalid input. I used this same
    function in another program similar to this one and it worked just fine, but
    it's not working in this one. Why? My code is included below. Your help
    will be greatly appreciated.

    <html>
    <head>
    <title>Syster Tara's Number Converter</title>
    <script language="JavaS cript">

    var bitString="";
    var hexString=""; // 6

    function validateInput(u serInput) // 8
    {
    if (isNaN(userInpu t)) // 10
    {
    alert("That's not a number. Try again."); // 12
    converter.numEn tered.focus(); // 13
    }
    else if (userInput.inde xOf(".") != -1) // 15
    {
    alert("That's not a whole number. Try again."); // 17
    converter.numEn tered.focus(); // 18
    }
    else if (parseInt(userI nput) < 0) // 20
    {
    alert("That's a negative number. Try a positive one"); // 22
    converter.numEn tered.focus(); // 23
    }
    else
    {
    return userInput;
    }
    }

    function determineBinSta rtPower(num) // 38
    {
    var power, multiple;

    num = validateInput(n um);
    num = parseInt(num); // 40
    alert("num = " + num); // 41

    power = Math.floor(Math .log(num)/Math.log(2)); // 43
    multiple = Math.pow(2, power); // 44
    bitString = bitString + "1"; // 45
    alert("power = " + power + "\nmultiple = " + multiple + "\nbitStrin g = " +
    bitString);
    num = num - multiple; // 47
    alert("num = " + num); // 48
    power--; // 49

    determineBinBit Val(num, power); // 51
    }

    function determineBinBit Val(leftover, exponent) // 54
    {
    var product; // 56
    alert("leftover = " + leftover + "\nexponent = " + exponent + "\nproduct =
    " + product);
    for(; exponent >= 0; exponent--) // 58
    {
    product = Math.pow(2, exponent); // 60
    if (leftover >= product) // 61
    {
    bitString = bitString +"1"; // 63
    leftover = leftover - product; // 64
    alert("leftover = " + leftover + "\nexponent = " + exponent + "\nproduct = "
    + product +

    "\nbitStrin g = " + bitString); // 66
    }
    else
    {
    bitString = bitString + "0"; // 70
    alert("leftover = " + leftover + "\nexponent = " + exponent + "\nproduct = "
    + product +

    "\nbitStrin g = " + bitString);
    }

    }
    document.write( "The binary value of " + converter.numEn tered.value + " is
    " + bitString);
    }


    function determineHexSta rtPower(num)
    {
    var power, multiple, quotient, firstDigit;

    num = parseInt(valida teInput(num));
    alert("num = " + num);

    power = Math.floor(Math .log(num)/Math.log(16));
    multiple = Math.pow(16, power);
    quotient = Math.floor(num / multiple);
    firstDigit = determineHexDig it(quotient);
    hexString = hexString + firstDigit.toSt ring();
    alert("power = " + power + "\nmultiple = " + multiple + "\nhexStrin g = " +
    hexString);
    num = num - (multiple * quotient);
    alert("num = " + num);
    power--;

    determineHexPla ceVal(num, power);

    }


    function determineHexPla ceVal(leftover, exponent)
    {
    var product, placeVal, hexDigit;

    alert("leftover = " + leftover + "\nexponent = " + exponent + "\nproduct =
    " + product);
    for(; exponent >= 0; exponent--)
    {
    product = Math.pow(16, exponent);
    alert("product = " + product + " after using the Math.pow fn");
    if (leftover >= product)
    {
    placeVal = Math.floor(left over / product);
    alert("placeVal = " + placeVal + "\nleftover = " + leftover +
    "\nproduct = " + product);
    hexDigit = determineHexDig it(placeVal);
    alert("hexDigit = " + hexDigit);
    hexString = hexString + hexDigit.toStri ng();
    leftover = leftover - (product * placeVal);
    alert("leftover = " + leftover + "\nexponent = " + exponent + "\nproduct = "
    + product +

    "\nhexStrin g = " + hexString);
    }
    else
    {
    hexString = hexString + "0";
    alert("leftover = " + leftover + "\nexponent = " + exponent + "\nproduct = "
    + product +

    "\nhexStrin g = " + hexString);
    }

    }
    document.write( "The hexadecimal value of " + converter.numEn tered.value +
    " is " + hexString);
    }


    function determineHexDig it(num)
    {
    alert("num = " + num);

    switch (num)
    {
    case 10:
    return "A";
    break;
    case 11:
    return "B";
    break;
    case 12:
    return "C";
    break;
    case 13:
    return "D";
    break;
    case 14:
    return "E";
    break;
    case 15:
    return "F";
    break;
    default:
    return num;
    }
    }


    </script>
    </head>

    <body>

    <h1 align="center"> Syster Tara's Number Converter</h1>
    <h3>Please enter a positive integer into the text box and click either the
    left button to get the

    number's binary value or the right button to get its hexadecimal value</h3>

    <form name="converter ">

    <table border="0" cellpadding="5" cellspacing="5" >
    <tr>

    <td><a href="http://www.spacefem.co m/blobs/"><img

    src="http://www.maethos.inf o/~spacefem/pinkblob.gif" width="90" height="98"
    border="0" alt="Adopt

    your own useless blob!"></a></td>

    <td><input type="text" name="numEntere d"></td>

    <td><input type="button" name="getBin" value="Get Binary Value"

    onclick="determ ineBinStartPowe r(numEntered.va lue)"></td>

    <td><input type="button" name="getHex" value="Get Hexadecimal Value"

    onclick="determ ineHexStartPowe r(numEntered.va lue)"></td>

    </tr>
    </table>
    </body>
    </html>


  • Vjekoslav Begovic

    #2
    Re: Need help with focus()

    Your focus() works just fine, but you didn't tell your script to stop
    executing in case of invalid input. validateInput() should return true if
    succeed, and false if don't. I've modified your code:

    1.
    function validateInput(u serInput) // 8
    {
    if (isNaN(userInpu t)) // 10
    {
    alert("That's not a number. Try again."); // 12
    converter.numEn tered.focus(); // 13
    return false; //validate failed
    }
    else if (userInput.inde xOf(".") != -1) // 15
    {
    alert("That's not a whole number. Try again."); // 17
    converter.numEn tered.focus(); // 18
    return false; //validate failed
    }
    else if (parseInt(userI nput) < 0) // 20
    {
    alert("That's a negative number. Try a positive one"); // 22
    converter.numEn tered.focus(); // 23
    return false;//validate failed
    }
    else
    {
    return true; //validate pass
    }
    }.

    2.
    function determineBinSta rtPower(num) // 38
    {
    var power, multiple;
    if (!validateInput (num)) return;
    num = parseInt(num); // 40
    alert("num = " + num); // 41

    power = Math.floor(Math .log(num)/Math.log(2)); // 43
    multiple = Math.pow(2, power); // 44
    bitString = bitString + "1"; // 45
    alert("power = " + power + "\nmultiple = " + multiple + "\nbitStrin g = " +
    bitString);
    num = num - multiple; // 47
    alert("num = " + num); // 48
    power--; // 49

    determineBinBit Val(num, power); // 51
    }


    "TSK" <susannek@minds pring.com> wrote in message
    news:bdhkss$cfp $1@slb6.atl.min dspring.net...[color=blue]
    > I cannot get my focus() fn to work in my validateInput(u serInput) fn. It
    > will not allow the user to go back and correct invalid input like it[/color]
    should.[color=blue]
    > Instead it goes right on through with the invalid input. I used this same
    > function in another program similar to this one and it worked just fine,[/color]
    but[color=blue]
    > it's not working in this one. Why? My code is included below. Your help
    > will be greatly appreciated.
    >
    > <html>
    > <head>
    > <title>Syster Tara's Number Converter</title>
    > <script language="JavaS cript">
    >
    > var bitString="";
    > var hexString=""; // 6
    >
    > function validateInput(u serInput) // 8
    > {
    > if (isNaN(userInpu t)) // 10
    > {
    > alert("That's not a number. Try again."); // 12
    > converter.numEn tered.focus(); // 13
    > }
    > else if (userInput.inde xOf(".") != -1) // 15
    > {
    > alert("That's not a whole number. Try again."); // 17
    > converter.numEn tered.focus(); // 18
    > }
    > else if (parseInt(userI nput) < 0) // 20
    > {
    > alert("That's a negative number. Try a positive one"); // 22
    > converter.numEn tered.focus(); // 23
    > }
    > else
    > {
    > return userInput;
    > }
    > }
    >
    > function determineBinSta rtPower(num) // 38
    > {
    > var power, multiple;
    >
    > num = validateInput(n um);
    > num = parseInt(num); // 40
    > alert("num = " + num); // 41
    >
    > power = Math.floor(Math .log(num)/Math.log(2)); // 43
    > multiple = Math.pow(2, power); // 44
    > bitString = bitString + "1"; // 45
    > alert("power = " + power + "\nmultiple = " + multiple + "\nbitStrin g = "[/color]
    +[color=blue]
    > bitString);
    > num = num - multiple; // 47
    > alert("num = " + num); // 48
    > power--; // 49
    >
    > determineBinBit Val(num, power); // 51
    > }
    >
    > function determineBinBit Val(leftover, exponent) // 54
    > {
    > var product; // 56
    > alert("leftover = " + leftover + "\nexponent = " + exponent + "\nproduct[/color]
    =[color=blue]
    > " + product);
    > for(; exponent >= 0; exponent--) // 58
    > {
    > product = Math.pow(2, exponent); // 60
    > if (leftover >= product) // 61
    > {
    > bitString = bitString +"1"; // 63
    > leftover = leftover - product; // 64
    > alert("leftover = " + leftover + "\nexponent = " + exponent + "\nproduct =[/color]
    "[color=blue]
    > + product +
    >
    > "\nbitStrin g = " + bitString); // 66
    > }
    > else
    > {
    > bitString = bitString + "0"; // 70
    > alert("leftover = " + leftover + "\nexponent = " + exponent + "\nproduct =[/color]
    "[color=blue]
    > + product +
    >
    > "\nbitStrin g = " + bitString);
    > }
    >
    > }
    > document.write( "The binary value of " + converter.numEn tered.value + "[/color]
    is[color=blue]
    > " + bitString);
    > }
    >
    >
    > function determineHexSta rtPower(num)
    > {
    > var power, multiple, quotient, firstDigit;
    >
    > num = parseInt(valida teInput(num));
    > alert("num = " + num);
    >
    > power = Math.floor(Math .log(num)/Math.log(16));
    > multiple = Math.pow(16, power);
    > quotient = Math.floor(num / multiple);
    > firstDigit = determineHexDig it(quotient);
    > hexString = hexString + firstDigit.toSt ring();
    > alert("power = " + power + "\nmultiple = " + multiple + "\nhexStrin g = "[/color]
    +[color=blue]
    > hexString);
    > num = num - (multiple * quotient);
    > alert("num = " + num);
    > power--;
    >
    > determineHexPla ceVal(num, power);
    >
    > }
    >
    >
    > function determineHexPla ceVal(leftover, exponent)
    > {
    > var product, placeVal, hexDigit;
    >
    > alert("leftover = " + leftover + "\nexponent = " + exponent + "\nproduct[/color]
    =[color=blue]
    > " + product);
    > for(; exponent >= 0; exponent--)
    > {
    > product = Math.pow(16, exponent);
    > alert("product = " + product + " after using the Math.pow fn");
    > if (leftover >= product)
    > {
    > placeVal = Math.floor(left over / product);
    > alert("placeVal = " + placeVal + "\nleftover = " + leftover +
    > "\nproduct = " + product);
    > hexDigit = determineHexDig it(placeVal);
    > alert("hexDigit = " + hexDigit);
    > hexString = hexString + hexDigit.toStri ng();
    > leftover = leftover - (product * placeVal);
    > alert("leftover = " + leftover + "\nexponent = " + exponent + "\nproduct =[/color]
    "[color=blue]
    > + product +
    >
    > "\nhexStrin g = " + hexString);
    > }
    > else
    > {
    > hexString = hexString + "0";
    > alert("leftover = " + leftover + "\nexponent = " + exponent + "\nproduct =[/color]
    "[color=blue]
    > + product +
    >
    > "\nhexStrin g = " + hexString);
    > }
    >
    > }
    > document.write( "The hexadecimal value of " + converter.numEn tered.value[/color]
    +[color=blue]
    > " is " + hexString);
    > }
    >
    >
    > function determineHexDig it(num)
    > {
    > alert("num = " + num);
    >
    > switch (num)
    > {
    > case 10:
    > return "A";
    > break;
    > case 11:
    > return "B";
    > break;
    > case 12:
    > return "C";
    > break;
    > case 13:
    > return "D";
    > break;
    > case 14:
    > return "E";
    > break;
    > case 15:
    > return "F";
    > break;
    > default:
    > return num;
    > }
    > }
    >
    >
    > </script>
    > </head>
    >
    > <body>
    >
    > <h1 align="center"> Syster Tara's Number Converter</h1>
    > <h3>Please enter a positive integer into the text box and click either the
    > left button to get the
    >
    > number's binary value or the right button to get its hexadecimal[/color]
    value</h3>[color=blue]
    >
    > <form name="converter ">
    >
    > <table border="0" cellpadding="5" cellspacing="5" >
    > <tr>
    >
    > <td><a href="http://www.spacefem.co m/blobs/"><img
    >
    > src="http://www.maethos.inf o/~spacefem/pinkblob.gif" width="90"[/color]
    height="98"[color=blue]
    > border="0" alt="Adopt
    >
    > your own useless blob!"></a></td>
    >
    > <td><input type="text" name="numEntere d"></td>
    >
    > <td><input type="button" name="getBin" value="Get Binary Value"
    >
    > onclick="determ ineBinStartPowe r(numEntered.va lue)"></td>
    >
    > <td><input type="button" name="getHex" value="Get Hexadecimal Value"
    >
    > onclick="determ ineHexStartPowe r(numEntered.va lue)"></td>
    >
    > </tr>
    > </table>
    > </body>
    > </html>
    >
    >[/color]


    Comment

    • Dr John Stockton

      #3
      Re: Need help with focus()

      JRS: In article <bdhkss$cfp$1@s lb6.atl.mindspr ing.net>, seen in
      news:comp.lang. javascript, TSK <susannek@minds pring.com> posted at Fri,
      27 Jun 2003 10:36:54 :-
      [color=blue]
      > My code is included below. Your help
      >will be greatly appreciated.[/color]

      Code for News should be written with 72-character margin, or otherwise
      arranged so that the sending newsreader does not wrap it. Why should we
      have to tediously reassemble the pieces when you could so easily have
      done it yourself?

      The whole method is far too long.

      <URL:http://www.merlyn.demo n.co.uk/js-maths.htm#Base> does a similar
      task in three lines of script :

      function BCvt() { with (document.forms['Frm1']) {
      out.value = parseInt(inp.va lue, inpbase.value).
      toString(outbas e.value).toUppe rCase() } }

      That converts from any base in 2..36 to any base in 2..36.

      You test your input number. Easier to test it with a RegExp :

      OK = /^\d+$/.test(inp.value ) // is it just 1 or more digits?

      after which +inp.value gets the value, as a Number, with no possible
      error (except too big, which you did not test).

      --
      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MSIE 4 ©
      <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

      Comment

      Working...