SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Mar 2003
    Location
    In a house in the USA
    Posts
    293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post Formatting windows product keys dynamically

    I am working on a project and they want to have the product keys automatically add the dashes and capitalize the key. The following code will work for that as long as you type the product key all at once and do not have to edit anything in the middle. My question is how would you "properly" format the product key in: xxxxx-xxxxx-xxxxx-xxxxx-xxxxx.

    Code:
    function productKey(data, e) {
      // Get the product key you are typing
      var productKey = data.value;
      // Capture the code for the key just pressed
      var KeyID = (window.event) ? event.keyCode : e.keyCode; 
      // Capitalize the product key
      productKey = productKey.toUpperCase();
      // Find the key length
      var keyLength = productKey.length;
      switch(KeyID) {
        case 8: // Backspace key was pressed
          // Determine if we need to remove an extra character due to 
          // removing the slash that we automatically added
          switch(keyLength % 6) {
            case 5:
              if(keyLength>4) {
                // Delete one extra character
                productKey = productKey.substr(0, (keyLength-1));
              }
              break;
          }      
          break;
        default:
          // Determine if we need to add a - or not
          switch(keyLength % 6) {
            case 5: // We just typed the 5th character in the 6th character string (including the -)
              if(keyLength<29) {
                // Add a - to the end of the product key
                productKey = productKey+'-';
              }
              break;
          } 
          break;
      }
      document.getElementById(data.id).value = productKey;
    }
    Then a textbox like:
    Code:
    <input type="text" name="key" id="key" onKeyUp="productKey(this, event)" value="" size="40">
    Any ideas?
    Daniel
    http://www.wlscripting.com - PHP Tutorials and code snippets
    Notepad++ Function List plugin tip - for PHP developers

  2. #2
    SitePoint Zealot
    Join Date
    Nov 2005
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The solution is simpler than using "window.event" to catch the key presses. Simply use the onkeypress event.

    Code:
    window.onload = function() {
      document.getElementById('field_1').keypress = format_keys;
    }
    
    function format_keys ()
    {
      // make input uppercase, and add dash after every x number of characters
    }
    You also can just use onchange or onblur event, especially if using onkeypress slows down typing response.


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
  •