9 Replies - 203 Views - Last Post: 29 May 2019 - 06:30 PM

#1 testmetestyou   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 94
  • Joined: 19-July 16

How to count product codes from textarea

Posted 26 May 2019 - 08:05 AM

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('\n');

	  event.target.value = values;
	}
	
	function saveToFile() {
	    console.log('Saving to the db');
	    toFile = $('#toFile').val().replace(/\n\r?/g, '<br />');
		$.ajax({
			url: "test2.php",
			type: "POST",
			data: {toFile:toFile}, // serializes the form's elements.
			beforeSend: function(xhr) {
	            // Let them know we are saving
				$('#status').html('Saving...');
			},
			success: function(toFile) {
				// You can get data returned from your ajax call here. ex. jqObj.find('.returned-data').html()
	            // Now show them we saved and when we did
	            var d = new Date();
	            $('#status').html('Saved! Last: ' + d.toLocaleTimeString());
			},
		});
	}

	$('.form').submit(function(e) {
		saveToFile();
		e.preventDefault();
	});



HTML:
<form class="contact-form" method="post">
    <textarea id="toFile" name="toFile" oninput="onInput()"></textarea><br>
    <input type="submit" value="Submit">
</form>

<!-- under here I want show count of product codes + overall count -->

<div id="status"></div>
<div id="info"></div>



This is how I want count product codes and overall:
https://pasteboard.co/IguZujW.png

This shows what you can only insert in textarea (Whitelist for product codes):
https://pasteboard.co/IguZHRe.png

Is This A Good Question/Topic? 0
  • +

Replies To: How to count product codes from textarea

#2 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2319
  • View blog
  • Posts: 7,073
  • Joined: 15-January 14

Re: How to count product codes from textarea

Posted 28 May 2019 - 10:12 AM

For a whitelist you just have a list of allowed terms and you check each one against that list, right? Which part of that do you need help with?
Was This Post Helpful? 0
  • +
  • -

#3 testmetestyou   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 94
  • Joined: 19-July 16

Re: How to count product codes from textarea

Posted 29 May 2019 - 05:06 AM

Yes, user can type only product codes what are in list.
LIST:
AAAB           -     Have 6 digit/char ending 000001
BBA             -     Have 8 digit/char ending 0000001P
CCCCCV     -     Have 3 digit/char ending 01R
L                  -     Have 7 digit/char ending 0000001


Textarea:
AAAB000001
AAAB000002
AAAB000003
BBA0000001P
BBA0000002P
BBA0000003P
BBA0000004P
CCCCCV01R
CCCCCV02R
CCCCCV03R
L0000001
L0000002


User cant type anything else what is not in the list.

I have example there but you cant use different lengths of product codes and endings, because length numbers are static there and its the issu there.
Link for Codepen
Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2319
  • View blog
  • Posts: 7,073
  • Joined: 15-January 14

Re: How to count product codes from textarea

Posted 29 May 2019 - 10:35 AM

It would probably be easiest to define your whitelist as a bunch of regular expressions, and test each line in the textarea against each one. Regular expressions are fairly expensive and that's going to take a fair amount of time if you need to test all of them for every line (i.e., if they enter a bunch of lines that are not valid), but that's probably your best option.
Was This Post Helpful? 0
  • +
  • -

#5 testmetestyou   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 94
  • Joined: 19-July 16

Re: How to count product codes from textarea

Posted 29 May 2019 - 02:40 PM

If this is so problematic and hard to do. Then maybe is secure to find code who have maximum length and then it's one line maximum length then you must go next line to type product code.
My head is blowing up. Some guys say it is easy to make.. I use in my code regexp but its only for one product code length, it cant be used different lengths of product codes.
Was This Post Helpful? 0
  • +
  • -

#6 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2319
  • View blog
  • Posts: 7,073
  • Joined: 15-January 14

Re: How to count product codes from textarea

Posted 29 May 2019 - 02:48 PM

You need one regular expression per product code pattern. Each regular expression matches a specific pattern. You could try to jam every pattern into one regular expression but that's going to be a pain to maintain if you ever need to change what's allowed. I would just do one regular expression per product code pattern.

It's not really that difficult though, it's just going to take time. You need to end up with an array of regular expressions that you loop through and test for each product code. The actual code is pretty simple, you just need to build the array of regular expressions first.
Was This Post Helpful? 0
  • +
  • -

#7 testmetestyou   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 94
  • Joined: 19-July 16

Re: How to count product codes from textarea

Posted 29 May 2019 - 03:53 PM

Another question if I have regular expression array how to I loop it for checking textarea?

code:
var regArray = new Array(<?php
	$data = $pdo->query("SELECT PRODUCT_CODE AS code, ENDING AS ec FROM test")->fetchAll(PDO::FETCH_OBJ);

	foreach ($data as $key) {
		$separator = ($key != end($data)) ? ", " : '';
		$std = "/^" . $key->code . "([a-zA-Z0-9]{" . $key->ec . "})$/";
		echo $std.$separator;
	}
?>);



result of array:
var regArray = new Array(/^AAAB([a-zA-Z0-9]{7})$/, /^BBBBBC([a-zA-Z0-9]{9})$/);


But is it good idea for checking what you can type into textarea automatically in realtime?
Because in source code you show still product codes..
Was This Post Helpful? 0
  • +
  • -

#8 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2319
  • View blog
  • Posts: 7,073
  • Joined: 15-January 14

Re: How to count product codes from textarea

Posted 29 May 2019 - 05:34 PM

You can use a for loop to loop through an array.

Quote

Because in source code you show still product codes..

No, you're just showing patterns. But why does that matter? How do you plan to check against a whitelist if you don't have the whitelist?
Was This Post Helpful? 0
  • +
  • -

#9 testmetestyou   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 94
  • Joined: 19-July 16

Re: How to count product codes from textarea

Posted 29 May 2019 - 05:37 PM

function onInput() {
    let values = event.target.value
    .split('\n')
    .map(v => {
        v = v.trim().toUpperCase();
        regArray.forEach(function(entry) {
              console.log(v);
            if (!v.match(entry)) {
                v = v.substr(0, v.length - 1);
              }
          });
         return v;
    }).join('\n');
      event.target.value = values;
}


Console log shows what key I pressed but it doesnt show result in textarea. 



Was This Post Helpful? 0
  • +
  • -

#10 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2319
  • View blog
  • Posts: 7,073
  • Joined: 15-January 14

Re: How to count product codes from textarea

Posted 29 May 2019 - 06:30 PM

And why do you think that is? You've decided to write a very concise piece of modern code for someone that didn't know how to loop through an array, a beginner probably wouldn't understand what that code does with all of the chained methods and the arrow function. So why isn't the input showing in the text area? It obviously registers the key press, right?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1