   /*
This sample code is provided by Annsa Ltd. for information only.
*/


var fieldMaximumLengthAttributeName = new String('exMaxLen');

//
// Limit the text input in the specified field.
//
function LimitInput(targetField)
{
    var isPermittedKeystroke;
    var enteredKeystroke;
    var maximumFieldLength;
    var currentFieldLength;
    var inputAllowed = true;
    var selectionLength = parseInt(document.selection.createRange().text.length);
    
    if ( targetField.getAttribute(fieldMaximumLengthAttributeName) != null )
    {
     // Get the current and maximum field length
     currentFieldLength = parseInt(targetField.value.length);
        maximumFieldLength = parseInt(targetField.getAttribute(fieldMaximumLengthAttributeName));

        // Allow non-printing, arrow and delete keys
        enteredKeystroke = window.event.keyCode;
        isPermittedKeystroke = ((enteredKeystroke < 32)                                // Non printing
                     ||(enteredKeystroke >= 33 && enteredKeystroke <= 40)    // Page Up, Down, Home, End, Arrow
                     ||(enteredKeystroke == 46))                            // Delete

        // Decide whether the keystroke is allowed to proceed
        if ( !isPermittedKeystroke )
        {
            if ( ( currentFieldLength - selectionLength ) >= maximumFieldLength ) 
            {
                inputAllowed = false;
            }
        }
    } 
    
    window.event.returnValue = inputAllowed;
    return (inputAllowed);
}
