[thelist] JS help needed - focus()

Hardacker, Andrew Andrew.Hardacker at Compuware.com
Thu Feb 21 09:35:01 CST 2002


<snip>
I am checking for numeric only values in the input boxes and when a
non-numeric character is entered, I
popup an alert box.  I would ideally like to return focus back to that box.
However, I can't seem to figure it out.
You can see the code at http://www.redhotsweeps.com/timesheet/
</snip>

Chris,

The only way I've ever gotten this to work properly is to use a timeout. I
also use "onchange" rather than "onblur" to initiate the check because if
you invoke "focus" programmatically, the browser also fires a "blur" event
to the object that's losing focus. It's a vicious circle.

You'll have to add an id attribute (or change the name to id. I assume this
is an intranet project, so you have some control over the browser.)

<input type="text" name="usps1" id="usps1" size="2" maxlength="5" value="30"
onFocus="this.select()" onchange="checkAmt(this)">

I notice that an employee can't enter a partial hour (no one at your company
works 13.5 hours on a Sunday? Got any openings?) Also, entering a space with
a number will cause your check to fail. This check goes a little farther. (I
got a little carried away, but had most of this code lying around.)

HTH

Andy
(Still on digest and usually behind. andrew.hardacker at compuware.com)

<script language="JavaScript">
var focusTo = null;
// This regular expression matches all whitespace characters and only
whitespace
var notjustwhitespace = /\S+/;
// This one allows one or two digits optionally followed by a decimal point
// and one or two digits.
var validnumber = /^\s*\d{0,2}([.]\d{0,2})?\s*$/;

function validateNumber(entry) {
  var myTime = entry.value;
  if (notjustwhitespace.test(myTime) && validnumber.test(myTime)) {
    var t = parseFloat(myTime);
    if (isNaN.t || t < 0.0 || t > 24.0) {
      return false;
    }
  } else return false;
  return true;
}

function checkAmt(entry){
// This will get the value of the passed variable and then add each one
// up across the screen (7 times) and then give a total.
  if (!validateNumber(entry)) {
    entry.style.backgroundColor = "yellow";
    alert("Invalid number of hours. Enter an amount between 0 and 24.");
    focusTo = entry;
    setTimeout('setFocus()',1);
  } else {
    entry.style.backgroundColor = "white";
    // pass group "name" (e.g., "usps") derived by stripping final digit
from id
    weeklyTotal(entry.id.substring(0,entry.id.length-1));
  }
}
function weeklyTotal(entryPrefix) {
    // loop 1 thru 7
    // add each value to a master value
    // spit out total in appropriate section
    var weeklyTotal = 0;
    var entry = 0;
    for (var i=1;i<8;i++) {
      entry = document.getElementById(entryPrefix+i);
      if (validateNumber(entry)) {
        weeklyTotal += parseFloat(entry.value);
      }
    }
    document.getElementById(entryPrefix+"total").value = weeklyTotal;
}
function setFocus() {
  if (focusTo != null) {
    focusTo.focus();
    focusTo = null;
  }
}
</script>





More information about the thelist mailing list