onchange event not firing while tabbing out of text box

Discussion in 'JavaScript' started by vivekrkv, Sep 27, 2006.

  1. #1
    Hi friends :)...............,<br>

    I have a problem where my onchange event is not firing at all. I have tried using both Firefox

    1.5.0.7 and IE 6.0.2900.<br><br>


    This is my code to verify that a text field has not been left blank:<br>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head><title>Check if the text field is empty</title>
    <meta http-equiv="Content-Type" content="text/html"; charset="EN-US";>
    <style type="text/css">
    </style>

    <script type="text/javascript">
    <!-- script hiding starts

    function checkTextField(field) {
    alert("entered function: checkTextField.");
    if ( isEmpty(field.value) ) {
    alert("Field is empty");
    }
    else {
    alert("Field is not empty and the value entered is: " + field.value + ".");
    }
    }

    //General Function to check if the text field is empty.
    function isEmpty(inputStr) {
    if ( inputStr == "" ) {
    return true;
    }
    else {
    return false;
    }
    }
    // script hiding ends -->
    </script>

    </head>
    <body>

    <form onsubmit="return false">
    Enter/Not-enter something to check whether the field is empty:
    <input type="text" name="checking" id="checking" value="" onchange="checkTextField(this)"

    />
    <input type="text" name="nothing" id="nothing" />
    </form>

    </body>
    </html>

    <!-- PROBLEM
    1.To validate if a text field is filled – when I tab out of that text field without entering any

    value, it does not
    fire the function. But when I fill in something and then tab out, it fires the function. And

    then immediately after
    this if I go back to that same field and delete everything and then tab out, then the function

    fires and tells me that
    there is no data in the field. Then immediately after this if I again go to that field and tab

    out, then the function does
    not fire. Its like this – the onchange event fires when data is entered, or when data is entered

    and for 1 time after
    that without data too. -->

    Thanks in advance.
    Cheers.
    vivek. :).................
     
    vivekrkv, Sep 27, 2006 IP
  2. Evoleto

    Evoleto Well-Known Member

    Messages:
    253
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    The event you're looking for is not onChange, it's onBlur ;)
     
    Evoleto, Sep 27, 2006 IP
  3. vivekrkv

    vivekrkv Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank You evoleto. The solution was so simple, can't believe it. The javascript bible that I was reading has so casually given onchange as the thing to be used, and they were worng!
     
    vivekrkv, Sep 28, 2006 IP
  4. emoore123

    emoore123 Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Can you provide the script please? I am having the exact same issue.

    Thanks,

    Mr. Ed.
     
    emoore123, Feb 27, 2008 IP