'JavaScript alert recognizing as same line and showing alert
In website, I made this. When I press enter, it recognize as new barcode in textarea.
In JavaScript, I made inserted digits are no more than 6 and Shows alert if more than 6 digits. But in progress; If I hit enter, there will be new barcode (example, 111111 and 222222 (in next line)) but JavaScript alert is recognized as more than 6 and keeps showing alert. But If I only insert 1 barcode, it works well. My opinion is that JS didn't know for the second inserted barcodes. May I know How can I fix?
$barcode_name = "Seal Number:";
$barname = "placeholder=\"Enter 6 digits Serial Number.\"";
<br>
<label class="control-label col-sm-4" for="seal_qty">Seal Quantity:</label>
<div class="col-sm-4">
<p class="form-control-static" style="margin-top: -6px;">
<input type="number" class="form-control" id="seal_qty" name="seal_qty" min="1" placeholder="Enter Seal Quantity" value="<?php echo $seal_qty; ?>">
</p>
</div>
<div class="col-sm-10"></div>
<br>
<label class="control-label col-sm-4" for="barcodeno"><?php echo $barcode_name; ?></label>
<div class="col-sm-4">
<p class="form-control-static" style="margin-top: -6px;">
<textarea class="form-control" rows="10" id="barcodeno" name="barcodeno" onkeydown="return KeyDown()" onkeyup="this.value=this.value.toUpperCase()" onkeypress="return isNumberKey(event)"<?php echo $barname; ?>></textarea>
</p>
</div>
<br>
Below is Javascript.
var barcodeno = document.translot.barcodeno.value;
if (barcodeno == ""){
alert("Please Insert Serial Numbers.")
return false;
}
else if (barcodeno.length != 6 ){
alert("Barcode Numbers must be at lest 6 digits.")
return false;
}
$('#filter').html(''); // to clear selection
// lotid = $('#lotid').val().split('\n');
var barcodeno = $('#barcodeno').val().trim();
barcodeno = barcodeno.split('\n');
//checking duplicate barcodeno
let barcodeno_array = []
for(let i = 0;i < barcodeno.length; i++){
if(barcodeno_array.indexOf(barcodeno[i]) == -1){
barcodeno_array.push(barcodeno[i])
}
}
var Barcode = barcodeno_array.length;
//alert (Barcode+'||'+seal_qty);
if (seal_qty != Barcode){
alert ("Seal Quantity and Number of BarcodeID must be the same.");
return false;
}
Solution 1:[1]
var barcodenoStr = $('#barcodeno').val().trim();
let barcodes = barcodeStr.split("\n");
let allBarcodeValid = true;
let barcodeno_array = []
for(let i =0; i < barcodes.length; i++){
let barcodeno = barcodes[i];
if (barcodeno == "" || barcodeno.length != 6){
allBarcodeValid = false;
break;
}
//checking duplicate barcodeno
if(barcodeno_array.indexOf(barcodes[i]) == -1){
barcodeno_array.push(barcodes[i])
}
}
if (!allBarcodeValid){
alert("Entered barcodes are not valid");
return false;
}
$('#filter').html(''); // to clear selection
// seal_qty variable is missing
if (seal_qty != barcodeno_array.length;){
alert ("Seal Quantity and Number of BarcodeID must be the same.");
return false;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
