Skip to main content
Inspiring
April 30, 2017
Answered

Validation script

  • April 30, 2017
  • 2 replies
  • 691 views

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 

This topic has been closed for replies.
Correct answer beh_gras_beh

This works,

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( '' )

    ;

};

event.value = "*" + encode(event.value, false) + "*";

2 replies

Inspiring
April 30, 2017

I have created a font wich can be used to create an EAN-13 barcode which is a superset of the UPC-A barcode.

How to use

EAN-13 has 3 symbol sets A, B, and C. It looks like the following: *XXXXXX#CCCCCC*

* start/end bar (as common for Code 39)

# middle bar

X a symbol of set A or B (see below)

C a symbol of set C

Symbol sets

The symbol sets are placed on an international keyboard layout:

Number row: 1234567890

Middle character row: asdfghjkl;

Top character row: qwertyuiop

The first digit of an EAN-13 barcode does not directly correspond to a symbol. It determines which symbol set for the following 6 digits is to be used. The following listing shows the symbol set combinations for the starting digits:

AABABB

AABBAB

AABBBA

ABAABB

ABBAAB

ABBBAA

ABABAB

ABABBA

ABBABA

If the starting digit is 0, or if you want to encode a UPC-A, only use symbol set A.

Examples

The spaces are only inserted for clarity.

EAN number → symbol set → What you type with the font

2 012345 67890 3 → *AABBAB#CCCCCC* → *01sd4g#yuiope*

2 230154 67890 1 → *AABBAB#CCCCCC* → *23;a5f#yuiopq*

9 786789 12345 1 → *ABBABA#CCCCCC* → *7kh7k9#qwertq*

9 789876 12345 7 → *ABBABA#CCCCCC* → *7kl8j6#qwertu*

Only can I do this in a form field?

beh_gras_behAuthorCorrect answer
Inspiring
April 30, 2017

This works,

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( '' )

    ;

};

event.value = "*" + encode(event.value, false) + "*";

JR Boulay
Community Expert
Community Expert
April 30, 2017

Hi.

What problems?

What this script is expected to do?

Acrobate du PDF, InDesigner et Photoshopographe