﻿var isValid = true;

onerror=handleError;
function handleError(msg,url,l)
{
    if(msg.toLowerCase() ==  "out of memory")
    {
        //als de key ingedrukt blijft krijg je een out of memory op de method MatchesMask
        return true;
    }
   
    //show
    return false;
    //don't show
    //return true;
}

function MatchesMask(control, mask)
{   
    if(control == null)
        return;
    //set the max length    
    control.maxLength = mask.length;
        
    //initialize with each check
    isValid = true;
    
    //get the pressed key value
    var value = String.fromCharCode(window.event.keyCode);
    isValid = ! ( (value.length == 0) && ((control.value.length + 1) > mask.length));// + 1 : the keypress char 
    
    if(isValid)
    {
        //get the karaktertype to check
        var charToCheck = mask.charAt(control.value.length);
        switch(charToCheck)
        {
            case '0':
                isValid = ! isNaN(value);
                break;
            case '#': //alfanumeriek, alles toegestaan
                break;
            default://overige moeten overeenstemmen
                isValid = value == charToCheck;
                if(! isValid)
                {
                    control.value = control.value + charToCheck;
                    //reasign the event 
                    // don't forget to  break here !! watch fot stackoverflow !!
                    MatchesMask(control, mask);
                    return;
                }
                break;
        }
    }
    if(! isValid)
        event.returnValue = false; 
}

