SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Dec 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    onblur validate focus

    I have an AJAX application for a form that has 4 columns and 170 rows. It uses an onblur call to update a database each time you move out of a cell. This works fine.

    Client wanted to validate for a number so I added a function to check for this and an alert call if the numer check failed. This works fine.

    However, dismissing the alert results in the focus moving to the next cell rather than returning to the invalid cell. Big problem. I have been successful in coding focus to go to any other cell in the table except the one with the errror.

    Here is the pertinent table code:

    <td width="75">
    <input type="text" name="depletions<?PHP echo $ti1; ?>" tabindex="<?PHP echo $ti1; ?>" id="depletions<?PHP echo $ti1; ?>" class="validate required depletions|<?PHP echo $id; ?> <?PHP echo $dbid; ?> usermsg depletions<?PHP echo $ti1; ?>" value="<?PHP echo $row['depletions']; ?>" size="8" /></td>
    <td width="75">
    <input type="text" name="endinginventory" tabindex="<?PHP echo $ti2; ?>" id="endinginventory" class="validate required endinginventory|<?PHP echo $id; ?> <?PHP echo $dbid; ?> usermsg" value="<?PHP echo $row['endinginventory']; ?>" size="8" /></td>

    Here is the the function in my javascript file:

    function validateMe(objInput) {

    sVal = objInput.value; //get value inside of input field

    sRules = objInput.className.split(' '); // get all the rules from the input box classname
    sRequired = sRules[1]; // determines if field is required or not
    sTypeCheck = sRules[2]; //typecheck are additional validation rules (ie. email, phone, date)
    gShow = sRules[4]; //gShow is the td id where feedback is sent to.
    sTable = sRules[3]; // database table to access
    sSource = sRules[5];

    if (isNaN (sVal)) {
    alert("Please enter a number.");
    document.form1.sSource.focus(); // <- THIS IS WHERE I SEEM TO BE HAVING A PROBLEM
    // I HAVE ALSO TRIED THE FOLLOWING:
    // document.form1[sSource].focus();
    // document.forms1.objInput.focus();
    return false;
    }

    //sends the rules and value to the asp page to be validated
    http.open("GET", sUrl + (sVal) + "&sRequired=" + (sRequired) + "&sTypeCheck=" + sTypeCheck +"&sTable=" + sTable, true);

    http.onreadystatechange = handleHttpResponse; // handle what to do with the feedback
    http.send(null);
    }

    Any help in getting focus on the correct cell when the alert is dismissed will be greatly appreciated.

  2. #2
    SitePoint Member
    Join Date
    Dec 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm reply to my own thread since I have found a partial solution that leads to a follow-up question.

    I have found a partial solution that works in IE and Opera, but fails in all versions of Firefox. The solution is:

    objInput.focus();

    So, now does anyone have a solution that will work in Firefox?

    Thanks.

  3. #3
    SitePoint Member
    Join Date
    Dec 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The solution turns out to be:

    setTimeout(function(){objInput.focus()}, 100);

    Hopes this helps somebody else.

  4. #4
    SitePoint Member
    Join Date
    May 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks a lot !! This helped me....

    Quote Originally Posted by apadley View Post
    The solution turns out to be:

    setTimeout(function(){objInput.focus()}, 100);

    Hopes this helps somebody else.
    Thanks a lot !! This helped me....


Bookmarks

Posting Permissions

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