SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot maileen's Avatar
    Join Date
    Apr 2006
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question onkeypress and input

    Hi,

    I have an <input> text field where user can type his password.
    in this <input> tag i have an onkeypress function which execute a javascript function.

    this js function check the number of characters typed. But there is a little issue.

    when user has type the 2nd character, the function detect only 1 character.
    ...
    when user has type 10 characters, the function detect only 9 characters.

    It's like the latest typed character is never sent to my js function.
    here after you can find a sample.

    thanks a lot,

    M.


    // in my PHP page
    <INPUT class='box600_form_field_text' TYPE='password' id='field_newpwd2' name='my_newpwd2' value='' onkeypress='CheckPwdStrength(this.value, \"pwd_img_strength\");'></INPUT>


    // my js function
    function CheckPwdStrength(pwd, image_id)
    {

    // get object of image
    bargraph = document.getElementById(image_id);

    if(pwd.length >= 8) // strong
    {
    // check if uppercase / lower case

    bargraph.src='../../images/icon/greenpwd_analyzer.gif';

    // check if number
    }
    else if (pwd.length >= 6) // average
    {
    bargraph.src='../../images/icon/yellowpwd_analyzer.gif';
    }
    else if (pwd.length >= 4) // weak
    {
    bargraph.src='../../images/icon/orangepwd_analyzer.gif';
    }
    else if (pwd.length <4) // very weak
    {
    bargraph.src='../../images/icon/redpwd_analyzer.gif';
    }

    }

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use onkeyup instead.

  3. #3
    SitePoint Zealot maileen's Avatar
    Join Date
    Apr 2006
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot it works.

    so where is the problem in onkeypress ? Did it get the last key pressed or does it run js script immediatelly without waiting the last key ?

    M.

  4. #4
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, something like that

  5. #5
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Order of events, if a textbox has the focus

    - user presses a key
    -* onkeydown event fires (this includes keys like shift and alt-f4)
    - onkeypress event fires (alphanumeric keys, stuff that would actually change the textbox)
    - character gets added to textbox
    - if user holds down key, goto *
    - user lets go of key
    - onkeyup event fires

    Note that from the onkeypress function you can get the value of the key being pressed from the event object.


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
  •