﻿
  function ValidateMachineCode(sender, args)
  {
    
    if (args.Value.length != 8)
        args.IsValid = false;
    else 
        args.IsValid = true;
  }

    
    
    //Restrict key input to only valid characters for a machine code
    //Example: onKeyPress="return keyRestrictToMachineCode(event)"
    function keyRestrictToMachineCode(e)
    {
        return keyRestrict(e, 'abcdefghijklmnopqrstuvwxyz');
    }

    //Restrict key input to only valid characters for a product key
    //Example: onKeyPress="return keyRestrictToProductKey(event)"
    function keyRestrictToProductKey(e)
    {
        return keyRestrict(e, '0123456789abcdef- ');
    }

    //Restrict the valid keys for a textbox to the list of specified keys
    //Example: onKeyPress="return keyRestrict(event,'1234567890')"
    function keyRestrict(e, validchars)
    {
        var key='', keychar='';
        key = getKeyCode(e);
        
        if (key == null) 
            return true;
        
        keychar = String.fromCharCode(key);
        keychar = keychar.toLowerCase();
        
        validchars = validchars.toLowerCase();
        
        if (validchars.indexOf(keychar) != -1)
            return true;
        
        if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
            return true;
        
        return false;
    }
        
    //Get the last keycode
    function getKeyCode(e)
    {
        if (window.event)
            return window.event.keyCode;
        else if (e)
            return e.which;
        else
            return null;
    }
