1. #1
    Ext JS Premium Member anton.dimitrov's Avatar
    Join Date
    Nov 2011
    Location
    The Netherlands
    Posts
    81
    Vote Rating
    1
    anton.dimitrov is on a distinguished road

      0  

    Default Ext.form.field.VTypes - IBAN

    Ext.form.field.VTypes - IBAN


    I checked the forums and couldn't find any IBAN validation so decided to do one by myself.
    The regular expressions are the same as the ones used in ZEND framework for validating IBAN(s) so I presume they work..

    Update: (30.05.2012)
    - fixed a bug with the replacing of characters. The characters must be treated as a regular expressions otherwise only the first occurrence is replaced.


    Code:
    Ext.apply(Ext.form.field.VTypes, {
    
            _ibanregex : {
                'AD' : '^AD[0-9]{2}[0-9]{8}[A-Z0-9]{12}$',
                'AT' : '^AT[0-9]{2}[0-9]{5}[0-9]{11}$',
                'BA' : '^BA[0-9]{2}[0-9]{6}[0-9]{10}$',
                'BE' : '^BE[0-9]{2}[0-9]{3}[0-9]{9}$',
                'BG' : '^BG[0-9]{2}[A-Z]{4}[0-9]{4}[0-9]{2}[A-Z0-9]{8}$',
                'CH' : '^CH[0-9]{2}[0-9]{5}[A-Z0-9]{12}$',
                'CS' : '^CS[0-9]{2}[0-9]{3}[0-9]{15}$',
                'CY' : '^CY[0-9]{2}[0-9]{8}[A-Z0-9]{16}$',
                'CZ' : '^CZ[0-9]{2}[0-9]{4}[0-9]{16}$',
                'DE' : '^DE[0-9]{2}[0-9]{8}[0-9]{10}$',
                'DK' : '^DK[0-9]{2}[0-9]{4}[0-9]{10}$',
                'EE' : '^EE[0-9]{2}[0-9]{4}[0-9]{12}$',
                'ES' : '^ES[0-9]{2}[0-9]{8}[0-9]{12}$',
                'FR' : '^FR[0-9]{2}[0-9]{10}[A-Z0-9]{13}$',
                'FI' : '^FI[0-9]{2}[0-9]{6}[0-9]{8}$',
                'GB' : '^GB[0-9]{2}[A-Z]{4}[0-9]{14}$',
                'GI' : '^GI[0-9]{2}[A-Z]{4}[A-Z0-9]{15}$',
                'GR' : '^GR[0-9]{2}[0-9]{7}[A-Z0-9]{16}$',
                'HR' : '^HR[0-9]{2}[0-9]{7}[0-9]{10}$',
                'HU' : '^HU[0-9]{2}[0-9]{7}[0-9]{1}[0-9]{15}[0-9]{1}$',
                'IE' : '^IE[0-9]{2}[A-Z0-9]{4}[0-9]{6}[0-9]{8}$',
                'IS' : '^IS[0-9]{2}[0-9]{4}[0-9]{18}$',
                'IT' : '^IT[0-9]{2}[A-Z]{1}[0-9]{10}[A-Z0-9]{12}$',
                'LI' : '^LI[0-9]{2}[0-9]{5}[A-Z0-9]{12}$',
                'LU' : '^LU[0-9]{2}[0-9]{3}[A-Z0-9]{13}$',
                'LT' : '^LT[0-9]{2}[0-9]{5}[0-9]{11}$',
                'LV' : '^LV[0-9]{2}[A-Z]{4}[A-Z0-9]{13}$',
                'MK' : '^MK[0-9]{2}[A-Z]{3}[A-Z0-9]{10}[0-9]{2}$',
                'MT' : '^MT[0-9]{2}[A-Z]{4}[0-9]{5}[A-Z0-9]{18}$',
                'NL' : '^NL[0-9]{2}[A-Z]{4}[0-9]{10}$',
                'NO' : '^NO[0-9]{2}[0-9]{4}[0-9]{7}$',
                'PL' : '^PL[0-9]{2}[0-9]{8}[0-9]{16}$',
                'PT' : '^PT[0-9]{2}[0-9]{8}[0-9]{13}$',
                'RO' : '^RO[0-9]{2}[A-Z]{4}[A-Z0-9]{16}$',
                'SE' : '^SE[0-9]{2}[0-9]{3}[0-9]{17}$',
                'SI' : '^SI[0-9]{2}[0-9]{5}[0-9]{8}[0-9]{2}$',
                'SK' : '^SK[0-9]{2}[0-9]{4}[0-9]{16}$',
                'TN' : '^TN[0-9]{2}[0-9]{5}[0-9]{15}$',
                'TR' : '^TR[0-9]{2}[0-9]{5}[A-Z0-9]{17}$'
            },
    
            _formatA: [
                'A',  'B',  'C',  'D',  'E',  'F',  'G',  'H',  'I',  'J',  'K',  'L',  'M',
                'N',  'O',  'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',  'X',  'Y',  'Z'
            ],
    
            _formatN: [
                '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22',
                '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35'
            ],
    
            _ibanUnsupportedRegionText: 'Unsupported IBNA region.',
            _ibanFalseFormatText: 'False format.',
            _ibanCheckFailText: 'IBAN check has failed.',
            _ibanDefultText: 'Unsupported IBNA region.',
            _setIbanError: function(v){
                this.IBANText = v;
            },
            IBANText: 'Unsupported IBNA region',
            IBANMask: /[a-z0-9_]/i,
    
            IBAN:  function(v) {
                v = v.toUpperCase();
                var me = this,
                    preg,
                    format,
                    temp,
                    len,
                    region = v.substr(0,2);
    
                if (!me._ibanregex[region]) {
                    me._setIbanError(me._ibanUnsupportedRegionText);
                    return false;
                }
    
                preg =  new RegExp(me._ibanregex[region]);
                if(!v.match(preg)){
                    me._setIbanError(me._ibanFalseFormatText);
                    return false;
                }
    
                format = v.substr(4) + v.substr(0,4);
                // optimizie...maybe...
                for(var x in me._formatA){
                    if(!!me._formatA[x] && !!me._formatN[x]){
                        var reg = new RegExp(me._formatA[x], 'g');
                        format = format.replace(reg,me._formatN[x]);
                    }
                }
                temp = parseInt(format.charAt(0));
                len  = format.length;
    
                for (var i = 1; i < len; i++) {
                    temp *= 10;
                    temp += parseInt(format.substr(i, 1));
                    temp %= 97;
                }
    
                if (temp != 1) {
                    me._setIbanError(me._ibanCheckFailText);
                    return false;
                }
                return true;
            }
        });
    Last edited by anton.dimitrov; 30 May 2012 at 9:17 AM. Reason: Fixed a bug

  2. #2
    Sencha - Support Team scottmartin's Avatar
    Join Date
    Jul 2010
    Location
    Houston, Tx
    Posts
    7,185
    Vote Rating
    194
    scottmartin is a splendid one to behold scottmartin is a splendid one to behold scottmartin is a splendid one to behold scottmartin is a splendid one to behold scottmartin is a splendid one to behold scottmartin is a splendid one to behold scottmartin is a splendid one to behold

      0  

    Default


    Thank you for the contribution!

    Regards,
    Scott

Tags for this Thread