IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JavaScript Discussion :

Décodeur de n° de série quasiment prêt


Sujet :

JavaScript

  1. #1
    Nouveau Candidat au Club
    Homme Profil pro
    Webmaster
    Inscrit en
    Juin 2022
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France

    Informations professionnelles :
    Activité : Webmaster

    Informations forums :
    Inscription : Juin 2022
    Messages : 3
    Points : 1
    Points
    1
    Par défaut Décodeur de n° de série quasiment prêt
    Bonjour à tous,

    Je suis webmaster et vraiment novice en js. J'ai réussi à créer un décodeur de n° de séries qui marche à 99%. Je cale toutefois sur un problème par rapport aux mois qui doivent être retournés sur le décodeur. Tous les mois de 1 à 0 (janvier à octobre) sont correctement retournés mais 'X': 'November', et '.': 'December', ne retourne rien. Sauriez-vous m'orienter vers la bonne solution pour les mois de novembre et décembre?


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    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
    36
    37
    38
    39
    40
    var monthStr = '';
            var month = search.substring(2, 3);
        var months = {
            '1': 'January',
            '2': 'February',
            '3': 'March',
            '4': 'April',
            '5': 'May',
            '6': 'June',
            '7': 'July',
            '8': 'August',
            '9': 'September',
            '0': 'October',
            'X': 'November',
            '.': 'December',
        };
     
        $.map(months, function(element, key) {
     
            if (key == month) {
                monthStr = element;
            }
        });
     
        if (!monthStr) {
            return error(search);
        }
     
        var day = search.substring(3, 5);
     
        if (isNaN(day)) {
            return error(search);
        }
        success(search, lineElement, '197' + year, monthStr, day);
     
    }
     
    function success(search, line, year, monthStr, day) {
     
        $("#panel_div").empty();
    Merci d'avance pour vos coups de mains!

  2. #2
    Expert éminent sénior

    Avatar de -Nikopol-
    Homme Profil pro
    Développeur Web
    Inscrit en
    Mai 2013
    Messages
    2 174
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France, Haute Marne (Champagne Ardenne)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : Enseignement

    Informations forums :
    Inscription : Mai 2013
    Messages : 2 174
    Points : 11 289
    Points
    11 289
    Billets dans le blog
    5
    Par défaut
    bonjour,
    le plus simple si j'ai bien compris (tu veux recuperer le mois en toute lettre ?) c'est d'aller chercher diretement dans l'object avec la clé :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    const months = {
                '1': 'January',
                '2': 'February',
                '3': 'March',
                '4': 'April',
                '5': 'May',
                '6': 'June',
                '7': 'July',
                '8': 'August',
                '9': 'September',
                '0': 'October',
                'X': 'November',
                '.': 'December',
            };
     
            monthStr = months['1']
            console.log(monthStr)
            monthStr = months['5']
            console.log(monthStr)
            monthStr = months['9']
            console.log(monthStr)
            monthStr = months['X']
            console.log(monthStr)
            monthStr = months['.']
            console.log(monthStr)

  3. #3
    Nouveau Candidat au Club
    Homme Profil pro
    Webmaster
    Inscrit en
    Juin 2022
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France

    Informations professionnelles :
    Activité : Webmaster

    Informations forums :
    Inscription : Juin 2022
    Messages : 3
    Points : 1
    Points
    1
    Par défaut
    Merci Nikopol pour ton aide. Oui, c'est exactement ça, je souhaite récupérer les mois en toute lettre. . Alors, je suis vraiment novice, du coup je ne sais pas avec quoi articuler ce code, avant et après... J'ai essayé ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    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
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
           var monthStr = '';
            var month = search.substring(2, 3);
            const months = {
                '1': 'January',
                '2': 'February',
                '3': 'March',
                '4': 'April',
                '5': 'May',
                '6': 'June',
                '7': 'July',
                '8': 'August',
                '9': 'September',
                '0': 'October',
                'X': 'November',
                '.': 'December',
            };
     
     
            monthStr = months['1']
            console.log(monthStr)
            monthStr = months['2']
            console.log(monthStr)
            monthStr = months['3']
            console.log(monthStr)
            monthStr = months['4']
            console.log(monthStr)
            monthStr = months['5']
            console.log(monthStr)
            monthStr = months['6']
            console.log(monthStr)
            monthStr = months['7']
            console.log(monthStr)
            monthStr = months['8']
            console.log(monthStr)
            monthStr = months['9']
            console.log(monthStr)
            monthStr = months['0']
            console.log(monthStr)
            monthStr = months['X']
            console.log(monthStr)
            monthStr = months['.']
            console.log(monthStr)
     
     
     
        var day = search.substring(3, 5);
     
        if (isNaN(day)) {
            return error(search);
        }
        success(search, lineElement, '197' + year, monthStr, day);
     
    }
     
    function success(search, line, year, monthStr, day) {
    Mais les mois me retournent [object Object] et 'X' et '.' retournent toujours comme avant "The serial can not be traced", message retourné quand le n° de série ne correspond pas.
    Désolé, je patauge encore bcp. Je n'avais jamais mis le nez dans du JS, ça fait seulement 5 jours.

  4. #4
    Expert éminent sénior

    Avatar de -Nikopol-
    Homme Profil pro
    Développeur Web
    Inscrit en
    Mai 2013
    Messages
    2 174
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France, Haute Marne (Champagne Ardenne)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : Enseignement

    Informations forums :
    Inscription : Mai 2013
    Messages : 2 174
    Points : 11 289
    Points
    11 289
    Billets dans le blog
    5
    Par défaut
    bonjour, montre nous ton code complet et des exemple de numéro à décoder ca sera plus facile de t'aider

  5. #5
    Nouveau Candidat au Club
    Homme Profil pro
    Webmaster
    Inscrit en
    Juin 2022
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France

    Informations professionnelles :
    Activité : Webmaster

    Informations forums :
    Inscription : Juin 2022
    Messages : 3
    Points : 1
    Points
    1
    Par défaut
    Bonjour Nikopol,

    Voilà mon code complet original avec 2 formats (logiques) de n° de série.

    Tout marche correctement SAUF les mois de novembre et décembre de "Function fisrtFormat" (à partir de la ligne 157).

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    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
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    $(function() {
        $('#ip1').keypress(function(event) {
            if (event.keyCode == 13) {
                return decoder();
            }
        });
     
     
    });
     
    function decoder() {
     
        var search = $('#ip1').val();
     
        if (search == 'Enter the serial number here') {
            search = '';
        }
        if (/^[a-zA-Z0-9- ]*$/.test(search) == false) {
            return error(search);
        }
     
     
        search = search.replace(/\s/g, '');
     
        if (search) {
     
            search = search.toUpperCase();
     
            switch (search.length) {
                case 5:
                    firstFormat(search);
                    break;
                case 6:
                    secondFormat(search);
                    break;
                default:
                    return error(search);
                    break;
     
            }
     
        }
     
        $("#ip1").focus();
        return false;
    }
     
     
     
    function secondFormat(search) {
     
     
            var lineStr = '';
        var line = search.substring(0, 1);
        var lines = {
            '1': 'Prototype line',
            '2': 'Production line 2',
     
        };
     
        var lineElement;
        var secondBeyond = search.substring(1);
     
        var isNumberSecondBeyond = /^\d+$/.test(secondBeyond);
        if (!isNumberSecondBeyond) {
     
            return error(search);
        }
     
     
        $.map(lines, function(element, key) {
     
            if (key == line) {
                lineElement = element;
            }
        });
     
        if (!lineElement) {
            return error(search);
        }
     
     
     
        var yearStr = '';
        var year = search.substring(1, 2);
        var years = {
            '0': '1980',
            '1': '1981',
            '8': '1978',
            '9': '1979',
     
        };
     
     
    var yearElement;
        var secondBeyond = search.substring(1);
     
        var isNumberSecondBeyond = /^\d+$/.test(secondBeyond);
        if (!isNumberSecondBeyond) {
     
            return error(search);
        }
     
     
        $.map(years, function(element, key) {
     
            if (key == year) {
                yearElement = element;
            }
        });
     
        if (!yearElement) {
            return error(search);
        }
     
     
     
            var monthStr = '';
            var month = search.substring(2, 4);
        var months = {
            '01': 'January',
            '02': 'February',
            '03': 'March',
            '04': 'April',
            '05': 'May',
            '06': 'June',
            '07': 'July',
            '08': 'August',
            '09': 'September',
            '10': 'October',
            '11': 'November',
            '12': 'December',
        };
     
        $.map(months, function(element, key) {
     
            if (key == month) {
                monthStr = element;
            }
        });
     
        if (!monthStr) {
            return error(search);
        }
     
        var day = search.substring(4, 6);
     
        if (isNaN(day)) {
            return error(search);
        }
        success(search, lineElement, yearElement, monthStr, day);
     
    }
     
     
     
    function firstFormat(search) {
     
            var lineStr = '';
        var line = search.substring(0, 1);
        var lines = {
            '1': 'Prototype line',
            '2': 'Production line 2',
            '3': 'Production line 3',
     
        };
     
        var lineElement;
        var secondBeyond = search.substring(1);
     
        var isNumberSecondBeyond = /^\d+$/.test(secondBeyond);
        if (!isNumberSecondBeyond) {
     
            return error(search);
        }
     
     
        $.map(lines, function(element, key) {
     
            if (key == line) {
                lineElement = element;
            }
        });
     
        if (!lineElement) {
            return error(search);
        }
     
     
     
        var yearStr = '';
        var year = search.substring(1, 2);
        var years = {
            2: '1972',
            3: '1973',
            4: '1974',
            5: '1975',
            6: '1976',
            7: '1977'
     
        };
     
     
        $.map(years, function(element, key) {
     
            if (key == year) {
                yearStr = element;
            }
        });
     
        if (!yearStr) {
            return error(search);
        }
     
     
            var monthStr = '';
            var month = search.substring(2, 3);
        var months = {
            '1': 'January',
            '2': 'February',
            '3': 'March',
            '4': 'April',
            '5': 'May',
            '6': 'June',
            '7': 'July',
            '8': 'August',
            '9': 'September',
            '0': 'October',
            'X': 'November',
            '.': 'December',
        };
     
        $.map(months, function(element, key) {
     
            if (key == month) {
                monthStr = element;
            }
        });
     
        if (!monthStr) {
            return error(search);
        }
     
        var day = search.substring(3, 5);
     
        if (isNaN(day)) {
            return error(search);
        }
        success(search, lineElement, '197' + year, monthStr, day);
     
    }
     
    function success(search, line, year, monthStr, day) {
     
        $("#panel_div").empty();
     
     
        var html = "<div align='center'>" +
            "<table align='center' border='0'>" +
            "<tr>" +
            "<td>&nbsp;</td>" +
            "<td>&nbsp;</td>" +
            "<td>&nbsp;</td>" +
            "</tr>" +
            "<tr>" +
            "<td><div align='right'><span style='font-size: 16px;font-family: Arial, Helvetica, sans-serif;'>Serial number:</span></div> </td>" +
            "<td width='5'>&nbsp;</td>" +
            "<td><div align='left'><span style='font-size: 16px;font-weight: bold;font-family: Arial, Helvetica, sans-serif;'>" + search + "</span></div></td>" +
            "</tr>" +
            "<tr>" +
            "<td>&nbsp;</td>" +
            "<td>&nbsp;</td>" +
            "<td>&nbsp;</td>" +
            "</tr>" +
                    "<tr>" +
            "<td><div align='right'><span style='font-size: 16px;font-family: Arial, Helvetica, sans-serif;'>Production line:</span></div></td>" +
            "<td width='5'>&nbsp;</td>" +
            "<td><div align='left'><span style='font-size: 16px;font-weight: bold;font-family: Arial, Helvetica, sans-serif;'>" + line + "</span></div></td>" +
            "</tr>" +
            "<tr>" +
            "<td><div align='right'><span style='font-size: 16px;font-family: Arial, Helvetica, sans-serif;'>Year of manufacture:</span></div></td>" +
            "<td width='5'>&nbsp;</td>" +
            "<td><div align='left'><span style='font-size: 16px;font-weight: bold;font-family: Arial, Helvetica, sans-serif;'>" + year + "</span></div></td>" +
            "</tr>" +
            "<tr>" +
            "<td><div align='right'><span style='font-size: 16px;font-family: Arial, Helvetica, sans-serif;'>Month:</span></div></td>" +
            "<td width='5'>&nbsp;</td>" +
            "<td><div align='left'><span style='font-size: 16px;font-weight: bold;font-family: Arial, Helvetica, sans-serif;'>" + monthStr + "</span></div></td>" +
            "</tr>" +
            "<tr>" +
            "<td><div align='right'><span style='font-size: 16px;font-family: Arial, Helvetica, sans-serif;'>Day of the month:</span></div></td>" +
            "<td width='5'>&nbsp;</td>" +
            "<td><div align='left'><span style='font-size: 16px;font-weight: bold;font-family: Arial, Helvetica, sans-serif;'>" + day + "</span></div></td>" +
            "</tr>" +
            "</table>" +
            "</div>";
     
     
        $("#panel_div").append(html);
        return false;
    }
     
    function error(search) {
        $("#panel_div").empty();
     
     
        var html = "<div align='center'>" +
            "<table border='0' align='center'>" +
            "<tr>" +
            "<td>&nbsp;</td>" +
            "<td>&nbsp;</td>" +
            "<td>&nbsp;</td>" +
            "</tr>" +
            "<tr>" +
            "<td><div align='right'><span style='font-size: 16px;font-family: Arial, Helvetica, sans-serif;'>Serial number:</span></div> </td>" +
            "<td width='5'>&nbsp;</td>" +
            "<td><div align='left'><span style='font-size: 16px;font-weight: bold;font-family: Arial, Helvetica, sans-serif;'>" + search + "</span></div></td>" + "</tr>" +
            "<tr>" +
            "<td>&nbsp;</td>" +
            "<td>&nbsp;</td>" +
            "<td>&nbsp;</td>" +
            "</tr>" +
            "<tr>" +
            "</table>" +
            "<table border='0'>" +
            "<tr>" +
            "<td><div align='center'><span style='font-size: 30px;color: #FF0000;font-family: Arial, Helvetica, sans-serif;'>This serial number can not be traced</span></div></td>" +
            "</tr>" +
            "</table>" +
            "</div>";
     
        $("#panel_div").append(html);
        return false;
    }
     
     
    function initial() {
        $("#panel_div").empty();
        $("#panel_div").append("<div align='center'><p> </p><p> </p><span style='font-size: 30px;color: #FF0000;'>This serial number can not be traced</span></div>");
        return false;
    }

  6. #6
    Modérateur

    Avatar de NoSmoking
    Homme Profil pro
    Inscrit en
    Janvier 2011
    Messages
    16 959
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations forums :
    Inscription : Janvier 2011
    Messages : 16 959
    Points : 44 122
    Points
    44 122
    Par défaut
    Bonjour,
    au vu de ton code il manque vraiment ...
    Citation Envoyé par -Nikopol-
    ... des exemple de numéro à décoder ...

Discussions similaires

  1. [Série] Accès au port série sous linux
    Par ghost dans le forum Entrée/Sortie
    Réponses: 10
    Dernier message: 10/10/2007, 10h43
  2. Numéro de série du disque dur
    Par h_kamel dans le forum Assembleur
    Réponses: 8
    Dernier message: 21/05/2007, 11h28
  3. Compression d'une série d'images jpeg
    Par Tchello dans le forum Langage
    Réponses: 3
    Dernier message: 31/08/2003, 19h59
  4. Créer une série dans un chart
    Par cyrose dans le forum C++Builder
    Réponses: 5
    Dernier message: 28/11/2002, 11h37
  5. Hors série PCTEAM sur Direct 3D
    Par Shakram dans le forum DirectX
    Réponses: 1
    Dernier message: 12/10/2002, 16h34

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo