Validation script
Dear people,
I'm having problems with a piece of script what I am trying to implement in a form field.
This is the code, only how can I apply this that the input is from the form field and the output automatically changes.
function encode( str, prepend )
{
var result = [];
if ( prepend ) {
result.push( str.charAt( 0 ) );
}
result.push( encode.first( str ) );
result.push( encode.second( str ) );
return result.join( ' ' ).trim();
}
encode.table = {
A: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
B: [';', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'],
C: ['p', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o']
}
encode.keys = [
['A', 'A', 'A', 'A', 'A', 'A'],
['A', 'A', 'B', 'A', 'B', 'B'],
['A', 'A', 'B', 'B', 'A', 'B'],
['A', 'A', 'B', 'B', 'B', 'A'],
['A', 'B', 'A', 'A', 'B', 'B'],
['A', 'B', 'B', 'A', 'A', 'B'],
['A', 'B', 'B', 'B', 'A', 'A'],
['A', 'B', 'A', 'B', 'A', 'B'],
['A', 'B', 'A', 'B', 'B', 'A'],
['A', 'B', 'B', 'A', 'B', 'A'],
];
encode.first = function ( str ) {
var key = encode.keys[ +str.charAt( 0 ) ];
return str
.substring( 1, 7 )
.split( '' )
.map(function ( number, index ) {
return encode.table[ key[ index ] ][ +number ];
})
.join( '' )
;
};
encode.second = function ( str ) {
return str
.substring( 7, 13 )
.split( '' )
.map(function ( number, index ) {
return encode.table.C[ +number ];
})
.join( '' )
;
};
encode('2230154678901', true) // 2 23;a5f yuiopq
