Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. how to count product codes from textarea?

how to count product codes from textarea?

Scheduled Pinned Locked Moved JavaScript
helptutorialjavascriptdata-structuresregex
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Veltix
    wrote on last edited by
    #1

    I want count in my textarea product codes.
    I have whitelist for product codes so I cant type or write anything else into textarea.
    Found bad issue, I can write only static product codes like (first 16 is product code other 6 is how many products it is in list...):

    0FAR12345H00123C + 00001P
    0FAR54321H00321C + 00001P
    0FAR54321H00321C + 00002P

    So now I have two problems..
    First problem, whitelist universal product codes like this:

    0FAR12345H00123C + 00001P
    0FAR543H00321C + 00001P
    0F321H321C + 00001P
    0R31H1C + 001P

    Second problem is how to count these product codes under textarea + overall count.

    Example under textarea counts:
    0FAR12345H00123C: 5
    0FAR543H00321C: 2
    0F321H321C: 6
    0R31H1C: 4
    Overall: 17

    Javascript:

    "use strict";
    toFile.focus();

    var timeoutId;
    $('#toFile').on('keypress', function() {
        console.log('Textarea Change');
        
        clearTimeout(timeoutId);
        timeoutId = setTimeout(function() {
            // Runs 3 second (3000 ms) after the last change    
            saveToFile();
        }, 3000);
    });
    
    toFile.addEventListener("keyup", event => {
        const data = toFile.value.split("\\n");
        const result = data.unique();
        info.textContent = result.length !== data.length ? "Duplicate removed" : "";
        toFile.value = result.join('\\n');
    });
    
    if (!Array.prototype.unique) {
        Object.defineProperty(Array.prototype, "unique", {
            configurable: false,  // these 3 properties default to false so not needed
            enumerable: false,    // But have added them just to show their availability.
            writable: false,         
            value: function() { 
                const existing = {};
                return this.filter(a => existing\[a\] = !existing\[a\] ? true : false);
            }
        });
    } else {
        throw new Error("Array.prototype.unique already defined.");
    }
    
    const CODE = \['Product', 'Codes', 'Here'\];
    const reg = /^(\[a-zA-Z0-9\]{1,6})$/;
    
    // Ensure values been inserted are in correct format
    function onInput() {
      let values = event.target.value
        .split('\\n')
        .map(v => {
          v = v.trim().toUpperCase();
          if (v.length <= 16) {
            if (CODE\[0\].substr(0, v.length) != v &&
              CODE\[1\].substr(0, v.length) != v) {
              v = v.substr(0, v.length - 1);
            }
          } else if (!v.substr(16, 22).match(reg)) { // at the moment static 16 product code lenght + 6 is count of products.
            v = v.substr(0, v.length - 1);
          }
          return v;
        }).join('
    
    J A 2 Replies Last reply
    0
    • V Veltix

      I want count in my textarea product codes.
      I have whitelist for product codes so I cant type or write anything else into textarea.
      Found bad issue, I can write only static product codes like (first 16 is product code other 6 is how many products it is in list...):

      0FAR12345H00123C + 00001P
      0FAR54321H00321C + 00001P
      0FAR54321H00321C + 00002P

      So now I have two problems..
      First problem, whitelist universal product codes like this:

      0FAR12345H00123C + 00001P
      0FAR543H00321C + 00001P
      0F321H321C + 00001P
      0R31H1C + 001P

      Second problem is how to count these product codes under textarea + overall count.

      Example under textarea counts:
      0FAR12345H00123C: 5
      0FAR543H00321C: 2
      0F321H321C: 6
      0R31H1C: 4
      Overall: 17

      Javascript:

      "use strict";
      toFile.focus();

      var timeoutId;
      $('#toFile').on('keypress', function() {
          console.log('Textarea Change');
          
          clearTimeout(timeoutId);
          timeoutId = setTimeout(function() {
              // Runs 3 second (3000 ms) after the last change    
              saveToFile();
          }, 3000);
      });
      
      toFile.addEventListener("keyup", event => {
          const data = toFile.value.split("\\n");
          const result = data.unique();
          info.textContent = result.length !== data.length ? "Duplicate removed" : "";
          toFile.value = result.join('\\n');
      });
      
      if (!Array.prototype.unique) {
          Object.defineProperty(Array.prototype, "unique", {
              configurable: false,  // these 3 properties default to false so not needed
              enumerable: false,    // But have added them just to show their availability.
              writable: false,         
              value: function() { 
                  const existing = {};
                  return this.filter(a => existing\[a\] = !existing\[a\] ? true : false);
              }
          });
      } else {
          throw new Error("Array.prototype.unique already defined.");
      }
      
      const CODE = \['Product', 'Codes', 'Here'\];
      const reg = /^(\[a-zA-Z0-9\]{1,6})$/;
      
      // Ensure values been inserted are in correct format
      function onInput() {
        let values = event.target.value
          .split('\\n')
          .map(v => {
            v = v.trim().toUpperCase();
            if (v.length <= 16) {
              if (CODE\[0\].substr(0, v.length) != v &&
                CODE\[1\].substr(0, v.length) != v) {
                v = v.substr(0, v.length - 1);
              }
            } else if (!v.substr(16, 22).match(reg)) { // at the moment static 16 product code lenght + 6 is count of products.
              v = v.substr(0, v.length - 1);
            }
            return v;
          }).join('
      
      J Offline
      J Offline
      jkirkerx
      wrote on last edited by
      #2

      Your pretty vague in saying what exactly your having trouble with. I'm not going to write code for you, but think of part numbers as words, gathering each word and then count the occurrence of each word. This is done all the time, using words like "apple, orange" so you get a count of "apples" and "oranges" Perhaps search the internet for a JavaScript example of counting words! Start again from scratch, and write functions to do the following. So get all the text in the textarea Gather and count each occurrence of a word and push the words into an array and increment the counter Then loop the array, and qualify the words as a part number, remove the ones that don't qualify. Now you have an array or words and the count. Then do something with the array of words. Now go back and write event listeners to trigger your main function.

      If it ain't broke don't fix it Discover my world at jkirkerx.com

      1 Reply Last reply
      0
      • V Veltix

        I want count in my textarea product codes.
        I have whitelist for product codes so I cant type or write anything else into textarea.
        Found bad issue, I can write only static product codes like (first 16 is product code other 6 is how many products it is in list...):

        0FAR12345H00123C + 00001P
        0FAR54321H00321C + 00001P
        0FAR54321H00321C + 00002P

        So now I have two problems..
        First problem, whitelist universal product codes like this:

        0FAR12345H00123C + 00001P
        0FAR543H00321C + 00001P
        0F321H321C + 00001P
        0R31H1C + 001P

        Second problem is how to count these product codes under textarea + overall count.

        Example under textarea counts:
        0FAR12345H00123C: 5
        0FAR543H00321C: 2
        0F321H321C: 6
        0R31H1C: 4
        Overall: 17

        Javascript:

        "use strict";
        toFile.focus();

        var timeoutId;
        $('#toFile').on('keypress', function() {
            console.log('Textarea Change');
            
            clearTimeout(timeoutId);
            timeoutId = setTimeout(function() {
                // Runs 3 second (3000 ms) after the last change    
                saveToFile();
            }, 3000);
        });
        
        toFile.addEventListener("keyup", event => {
            const data = toFile.value.split("\\n");
            const result = data.unique();
            info.textContent = result.length !== data.length ? "Duplicate removed" : "";
            toFile.value = result.join('\\n');
        });
        
        if (!Array.prototype.unique) {
            Object.defineProperty(Array.prototype, "unique", {
                configurable: false,  // these 3 properties default to false so not needed
                enumerable: false,    // But have added them just to show their availability.
                writable: false,         
                value: function() { 
                    const existing = {};
                    return this.filter(a => existing\[a\] = !existing\[a\] ? true : false);
                }
            });
        } else {
            throw new Error("Array.prototype.unique already defined.");
        }
        
        const CODE = \['Product', 'Codes', 'Here'\];
        const reg = /^(\[a-zA-Z0-9\]{1,6})$/;
        
        // Ensure values been inserted are in correct format
        function onInput() {
          let values = event.target.value
            .split('\\n')
            .map(v => {
              v = v.trim().toUpperCase();
              if (v.length <= 16) {
                if (CODE\[0\].substr(0, v.length) != v &&
                  CODE\[1\].substr(0, v.length) != v) {
                  v = v.substr(0, v.length - 1);
                }
              } else if (!v.substr(16, 22).match(reg)) { // at the moment static 16 product code lenght + 6 is count of products.
                v = v.substr(0, v.length - 1);
              }
              return v;
            }).join('
        
        A Offline
        A Offline
        ananyagupta
        wrote on last edited by
        #3

        When I am developing a character count for my text area on my website. Right now, it says NaN because it seems to not find the length of how many characters are in the field, at beginning start with 0, so the number should be 500. In the console in chrome developer tools, no error occurs on my code is on the site, after though tried to use jQuery a regular JavaScript for the character count for the text area field, but nothing seems to work for more click JAVA training in Delhi

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups