AWS Javascript SDK EC2 DescribeInstances - javascript

I am trying to get date & no. of instances spin-up on that day.
Here is my code , i am beginner in development & in javascript.
so forgive me in advance for any immature code.
after getting startdate & end date , dates are stored in listDate array.
hours of a day is stored in hourArray.
then using JS SDK for AWS to get no. of instances spinned up on specific date.
ec2.describeInstances is not working as expected here ,
sometimes , i got output from ec2.describeInstances correctly , but it is always picking end date in all while loop iterations.
most of the time , ec2.describeInstances doesnt even execute.
function applyFilters() {
var listDate = [];
demo1.innerHTML = '';
var startDate = document.getElementById("fromdate").value;
var endDate = document.getElementById("todate").value;
//alert(startDate);
//alert(endDate);
var dateMove = new Date(startDate);
var strDate = startDate;
while (strDate < endDate){
var strDate = dateMove.toISOString().slice(0,10);
listDate.push(strDate);
dateMove.setDate(dateMove.getDate()+1);
};
//alert(listDate);
//document.getElementById('demo1').innerHTML = "Your selected dates are";
//demo2.innerHTML += "<br>";
//document.getElementById('demo3').innerHTML = listDate;
alert(listDate);
var i = 0;
var j = listDate.length;
alert(j);
while (i < j){
alert(i);
alert(listDate[i]);
var hourArray = [];
var d = listDate[i];
for (var h = 0; h <= 9; h++) {
hourArray.push(d + 'T' + '0' + h + '*');
}
for (var m = 10; m <= 23; m++) {
hourArray.push(d + 'T' + m + '*');
}
alert(hourArray);
var ec2 = new AWS.EC2();
AWS.config.update({
region: "xxxxxxx",
accessKeyId: "xxxxxxxxxxxxxxxxxxxxx",
secretAccessKey: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
});
i++;
var params = {
Filters: [
{
Name: 'launch-time',
Values: hourArray
}]
};
//alert("Specified launch-times are: " + hourArray);
ec2.describeInstances(params, function(err, data, d) {
//alert(data);
if (err) {
//demo5.innerHTML = 'ERROR:' + err;
console.log(err);
} else {
var no_of_inst = data.Reservations.length;
//demo4.innerHTML += "<br>";
//document.getElementById('demo5').innerHTML = 'Instances migrated on' + listdate[i] + 'are: ' + no_of_inst;
alert("Instances migrated on" + d + "are: " + no_of_inst);
}
});
}
}
<!DOCTYPE html>
<html>
<head>
<title>Migration Status</title>
<!--<link rel="stylesheet" href="styles.css">-->
<div>
<h3> M I G R A T I O N - S T A T U S </h3>
</div>
<meta charset="utf-8">
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.208.0.min.js"></script>
</head>
<body>
<p>Select Filters to get your ec2 graph :</p>
<form>
<div class = "css-grid1">
<label> Launch-time: </label>
</div>
<div class = "css-inlineblock1">
<label for="fromdate">From Date:</label>
<input type="date" id="fromdate" name="fromdate" min="2017-01-01">
<label for="todate">To Date:</label>
<input type="date" id="todate" name="todate">
</div>
<div class = "css-button">
<input type="submit" id="sbmt" onclick="applyFilters()">
</div>
<div id="demo1"></div>
<div id="demo2"></div>
<div id="demo3"></div>
<div id="demo4"></div>
<div id="demo5"></div>
<div id="demo6"></div>
<div id="demo7"></div>
<div id="demo8"></div>
</form>
<script>
</script>
</body>
</html>

Related

Wrap function into loop

I have script that updates time for one tag if I use querySelector and updateTime(inputTime.value); but how do I create loop so it works for all elements using querySelectorAll.
I've tried for, foreach, but I just can't understand what's my mistake. I'm using moment js, timezone detection.
JS
if (!sessionStorage.getItem('timezone')) {
var tz = jstz.determine() || 'UTC';
sessionStorage.setItem('timezone', tz.name());
}
var currTz = sessionStorage.getItem('timezone');
var inputTime = document.querySelectorAll(".input-time");
var output = document.querySelectorAll(".local");
function updateTime(theTime) {
var date = moment().format("YYYY-MM-DD");
var stamp = date + "T" + theTime + "Z";
var momentTime = moment(stamp);
var tzTime = momentTime.tz(currTz);
var formattedTime = tzTime.format('h:mm A');
output.textContent = "Time in " + currTz + ": " + formattedTime;
}
var i;
for (i = 0; i < inputTime.length; ++i) {
updateTime(inputTime[i].value);
}
HTML
<label for="input-time">Time in UTC: </label>
<input value="12:30" type="time" class="input-time">
<p class="local">x</p>
<label for="input-time">Time in UTC: </label>
<input value="12:30" type="time" class="input-time">
<p class="local">x</p>

count number of character occurrences in a sentence

I'm writing a code where users can input any text and choose any letter to see how many times it occurred in that particular text - I'm not sure where I'm going wrong
function textOccurrences() {
var inputField1 = (document.getElementById("inputField1").value);
var inputField2 = (document.getElementById("inputField2").value);
var count = 0;
for (i = 0; i < length; i++) {
if (parseInt(search) != -1) {
count++;
var search = inputField1.indexOf(inputField2, parseInt(search) + 1);
}
document.getElementId("answer").value = inputField2 + "Occurs" + count + "times";
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> Text Occurrences </title>
<h1> Number of Character Occurrences</h1>
<script="text/javascript"> </script>
</head>
<body>
<form>
<p>Enter text: </p>
<p> <input id="inputField1" type="text" /> </p>
<p>Enter any character:</p>
<p> <input id="inputField2" type="text" /> </p>
<input type="button" value="Search" onlick="textOccurrences()" />
</p>
<p>Number of Occurrences:</p>
<p><textarea id="answer"></textarea></p>
</form>
</body>
</html>
Try this as your function.
function textOccurrences() {
var inputField1 = (document.getElementById("inputField1").value);
var inputField2 = (document.getElementById("inputField2").value);
var count = inputField1.split(inputField2).length - 1;
document.getElementId("answer").value = inputField2 + "Occurs" + count + "times";
}
You can use a regular expression:
function textOccurrences() {
var inputField1 = (document.getElementById("inputField1").value);
var inputField2 = (document.getElementById("inputField2").value);
var re = new RegExp(inputField2, "g");
var count = inputField1.match(re).length;
document.getElementById("answer").value = inputField2 + " Occurs " + count + " times";
}
Note that in your code, you have said getElementId instead of getElementById, also contributing to the error.
function textOccurrences() {
var inputField1 = (document.getElementById("inputField1").value);
var inputField2 = (document.getElementById("inputField2").value);
var reg = new RegExp(inputField2,'g');
document.getElementById("answer").value = inputField2 + "Occurs" + inputField1.match(reg).length +
"times";
}
You can try something like this
you can use regex to match any occurence of you chosen letter and then count those matches and you have to syntax errors in your html and javascript
-it is onclick NOT onlick
-and it is getElementById NOT getElementId
here is you code after editing
Javascript
function textOccurrences() {
var inputField1 = (document.getElementById("inputField1").value);
var inputField2 = (document.getElementById("inputField2").value);
console.log("hekasdlkjasd");
var re = new RegExp(inputField2, "g");
var count = inputField1.match(re).length;
document.getElementById("answer").value = inputField2 + " Occurs " + count + " times";
}
Html
<!DOCTYPE html>
<html>
<head>
<title> Text Occurrences </title>
<h1> Number of Character Occurrences</h1>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<form>
<p>Enter text: </p>
<p> <input id="inputField1" type="text" /> </p>
<p>Enter any character:</p>
<p> <input id="inputField2" type="text" /> </p>
<input type="button" value="Search" onclick="textOccurrences();" />
</p>
<p>Number of Occurrences:</p>
<p><textarea id="answer"></textarea></p>
</form>
</body>
</html>

Textarea does not want to clear (when button clicked)

I have just started out on learning JavaScript and HTML, and I am currently working on a pizza delivery program for a project. After the person gives an order (entering it into a form and clicking the order button) it is output into a text area.
If someone orders something else(click the order button again) the text area should be cleared, yet it does not clear. I have tried numerous things I found on SO and W3Schools, and yet it does not want to clear the text area.
Code to empty textarea:(Almost at the top lines within the orderPizzas function) The textarea is at the bottom of my code within the <body> with ID: Details
document.getElementById("Details").value = "";
Here is my code:
var pizzaCount = 0;
var gourmetPrice = 15.50;
var standardPrice = 9.50;
var deliveryCharge = 5;
var TotalPrice;
var name;
var adress;
var phoneNumber = 10;
var gourmetCount = 0;
var standardCount = 0;
var orderDetails = '';
function orderPizzas() {
var customerDetails;
var i = 0;
var j = 0;
TotalPrice = 0;
phoneNumber = '';
document.getElementById("Details").value = "";
var arrStandardPizza = new Array()
arrStandardPizza[0] = new Array();
arrStandardPizza[0]['name'] = 'Hawaiian';
arrStandardPizza[0]['amount'] = Number(document.standard.Hawaiian.value);
arrStandardPizza[1] = new Array();
arrStandardPizza[1]['name'] = 'Cheese';
arrStandardPizza[1]['amount'] = Number(document.standard.Cheese.value);
arrStandardPizza[2] = new Array();
arrStandardPizza[2]['name'] = 'Veggie';
arrStandardPizza[2]['amount'] = Number(document.standard.Veggie.value);
arrStandardPizza[3] = new Array();
arrStandardPizza[3]['name'] = 'Supreme';
arrStandardPizza[3]['amount'] = Number(document.standard.Supreme.value);
arrStandardPizza[4] = new Array();
arrStandardPizza[4]['name'] = 'Pepperoni';
arrStandardPizza[4]['amount'] = Number(document.standard.Pepperoni.value);
var arrGourmetPizza = new Array()
arrGourmetPizza[0] = new Array();
arrGourmetPizza[0]['name'] = 'Meatlovers';
arrGourmetPizza[0]['amount'] = Number(document.gourmet.Meatlovers.value);
arrGourmetPizza[1] = new Array();
arrGourmetPizza[1]['name'] = 'Chicken';
arrGourmetPizza[1]['amount'] = Number(document.gourmet.Chicken.value);
arrGourmetPizza[2] = new Array();
arrGourmetPizza[2]['name'] = 'Prawn';
arrGourmetPizza[2]['amount'] = Number(document.gourmet.Prawn.value);
standardCount = arrStandardPizza[0]['amount'] + arrStandardPizza[1]['amount'] + arrStandardPizza[2]['amount'] + arrStandardPizza[3]['amount'] + arrStandardPizza[4]['amount'];
gourmetCount = arrGourmetPizza[0]['amount'] + arrGourmetPizza[1]['amount'] + arrGourmetPizza[2]['amount'];
pizzaCount = standardCount + gourmetCount;
if (pizzaCount > 12) {
alert('A maximum of 12 pizzas can be ordered.\nPlease modify your order.\nPizzas ordered: ' + pizzaCount);
} else {
while (i < 5) {
if (arrStandardPizza[i]['amount'] > 0) {
orderDetails = orderDetails + '\n' + arrStandardPizza[i]['name'] + ': ' + arrStandardPizza[i]['amount'];
}
i++;
}
while (j < 3) {
if (arrGourmetPizza[j]['amount'] > 0) {
orderDetails = orderDetails + '\n' + arrGourmetPizza[j]['name'] + ': ' + arrGourmetPizza[j]['amount'];
}
j++;
}
if (document.getOrderMethod.method.value == 'Delivery') {
name = prompt('What is your name?');
adress = prompt('What is your adress?');
while (phoneNumber.toString().length !== 10) {
phoneNumber = prompt('What is your phone number?');
}
customerDetails = '\nDelivery:\n' + 'Name: ' + name + ' ' + '\n' + 'Adress: ' + adress + '\n' + 'Phone Number: ' + phoneNumber;
TotalPrice = deliveryCharge;
} else {
name = prompt('What is your name?');
customerDetails = '\nPick-up:\n' + 'Customer Name: ' + name;
}
TotalPrice = TotalPrice + (standardCount * standardPrice) + (gourmetCount * gourmetPrice);
orderDetails = orderDetails + customerDetails + '\n' + 'Total Cost: $' + TotalPrice;
document.getElementById("Details").value = orderDetails;
}
}
<!DOCTYPE html>
<html>
<head>
<title> Pete's Pizza </title>
</head>
<body>
<h1> Welcome to Pete's Pizzas, where the best pizzas are! </h1>
<h3> Enter your pizza order: </h3>
<label> Amount for each standard pizza </label>
<form name="standard">
<input type="text" name="Hawaiian"> Hawaiian Pizza <br>
<input type="text" name="Cheese"> Cheese Pizza <br>
<input type="text" name="Veggie"> Veggie Pizza <br>
<input type="text" name="Supreme"> Supreme Pizza <br>
<input type="text" name="Pepperoni"> Pepperoni Pizza <br>
</form>
<label> Amount for each gourmet pizza </label>
<form name="gourmet">
<input type="text" name="Meatlovers"> Meat-lovers Pizza <br>
<input type="text" name="Chicken"> Chicken Pizza <br>
<input type="text" name="Prawn"> Prawn <br>
</form>
<form name="getOrderMethod">
<input type="radio" name="method" value="Delivery" checked> Delivery <br>
<input type="radio" name="method" value="Pick-up"> Pick-up <br>
</form>
<input type="button" value="Confirm Order" onClick="orderPizzas()">
<input type="button" value="Cancel Order" onClick="window.location.reload()">
<textarea id="Details" value="" rows="9" cols="33" wrap=on readonly></textarea>
</body>
</html>
I am very new to JavaScript and HTML, all advice will be well-received. Thanks in advance.
It appears that the problem is because you have defined orderDetails outside your method. You need to clear the variable everytime or scope it locally, or you just keep appending to it.
Just move the variable declaration from global scope into the method definition and it ought to work.
You are not clearing the orderDetails.
orderDetails="";
in start of function "orderPizzas" use above code.
var pizzaCount = 0;
var gourmetPrice = 15.50;
var standardPrice = 9.50;
var deliveryCharge = 5;
var TotalPrice;
var name;
var adress;
var phoneNumber = 10;
var gourmetCount = 0;
var standardCount = 0;
function orderPizzas() {
var customerDetails;
var orderDetails = '';
var i = 0;
var j = 0;
TotalPrice = 0;
phoneNumber = '';
document.getElementById("Details").value = "";
var arrStandardPizza = new Array()
arrStandardPizza[0] = new Array();
arrStandardPizza[0]['name'] = 'Hawaiian';
arrStandardPizza[0]['amount'] = Number(document.standard.Hawaiian.value);
arrStandardPizza[1] = new Array();
arrStandardPizza[1]['name'] = 'Cheese';
arrStandardPizza[1]['amount'] = Number(document.standard.Cheese.value);
arrStandardPizza[2] = new Array();
arrStandardPizza[2]['name'] = 'Veggie';
arrStandardPizza[2]['amount'] = Number(document.standard.Veggie.value);
arrStandardPizza[3] = new Array();
arrStandardPizza[3]['name'] = 'Supreme';
arrStandardPizza[3]['amount'] = Number(document.standard.Supreme.value);
arrStandardPizza[4] = new Array();
arrStandardPizza[4]['name'] = 'Pepperoni';
arrStandardPizza[4]['amount'] = Number(document.standard.Pepperoni.value);
var arrGourmetPizza = new Array()
arrGourmetPizza[0] = new Array();
arrGourmetPizza[0]['name'] = 'Meatlovers';
arrGourmetPizza[0]['amount'] = Number(document.gourmet.Meatlovers.value);
arrGourmetPizza[1] = new Array();
arrGourmetPizza[1]['name'] = 'Chicken';
arrGourmetPizza[1]['amount'] = Number(document.gourmet.Chicken.value);
arrGourmetPizza[2] = new Array();
arrGourmetPizza[2]['name'] = 'Prawn';
arrGourmetPizza[2]['amount'] = Number(document.gourmet.Prawn.value);
standardCount = arrStandardPizza[0]['amount'] + arrStandardPizza[1]['amount'] + arrStandardPizza[2]['amount'] + arrStandardPizza[3]['amount'] + arrStandardPizza[4]['amount'];
gourmetCount = arrGourmetPizza[0]['amount'] + arrGourmetPizza[1]['amount'] + arrGourmetPizza[2]['amount'];
pizzaCount = standardCount + gourmetCount;
if (pizzaCount > 12) {
alert('A maximum of 12 pizzas can be ordered.\nPlease modify your order.\nPizzas ordered: ' + pizzaCount);
}
else {
while(i < 5) {
if ( arrStandardPizza[i]['amount'] > 0) {
orderDetails = orderDetails + '\n' + arrStandardPizza[i]['name'] + ': ' + arrStandardPizza[i]['amount'];
}
i++;
}
while(j < 3) {
if ( arrGourmetPizza[j]['amount'] > 0) {
orderDetails = orderDetails + '\n' + arrGourmetPizza[j]['name'] + ': ' + arrGourmetPizza[j]['amount'];
}
j++;
}
if (document.getOrderMethod.method.value == 'Delivery') {
name = prompt('What is your name?');
adress = prompt('What is your adress?');
while( phoneNumber.toString().length !== 10) {
phoneNumber = prompt('What is your phone number?');
}
customerDetails = '\nDelivery:\n' + 'Name: ' + name + ' ' + '\n' + 'Adress: ' + adress + '\n' + 'Phone Number: ' + phoneNumber;
TotalPrice = deliveryCharge;
}
else {
name = prompt('What is your name?');
customerDetails = '\nPick-up:\n' + 'Customer Name: ' + name;
}
TotalPrice = TotalPrice + (standardCount * standardPrice) + (gourmetCount * gourmetPrice);
orderDetails = orderDetails + customerDetails + '\n' + 'Total Cost: $' + TotalPrice;
document.getElementById("Details").value = orderDetails;
}
}
<h1> Welcome to Pete's Pizzas, where the best pizzas are! </h1>
<h3> Enter your pizza order: </h3>
<label> Amount for each standard pizza </label>
<form name ="standard">
<input type="text" name="Hawaiian" > Hawaiian Pizza <br>
<input type="text" name="Cheese" > Cheese Pizza <br>
<input type="text" name="Veggie" > Veggie Pizza <br>
<input type="text" name="Supreme" > Supreme Pizza <br>
<input type="text" name="Pepperoni" > Pepperoni Pizza <br>
</form>
<label> Amount for each gourmet pizza </label>
<form name ="gourmet">
<input type="text" name="Meatlovers" > Meat-lovers Pizza <br>
<input type="text" name="Chicken" > Chicken Pizza <br>
<input type="text" name="Prawn" > Prawn <br>
</form>
<form name="getOrderMethod">
<input type="radio" name="method" value="Delivery" checked> Delivery <br>
<input type="radio" name="method" value="Pick-up"> Pick-up <br>
</form>
<input type="button" value="Confirm Order" onClick="orderPizzas()" >
<input type="button" value="Cancel Order" onClick="window.location.reload()" >
<textarea id="Details" value="" rows="9" cols="33" wrap=on readonly>
</textarea>
CustomerDetails should be initialized every time the function is called.
Try it!

Uncaught rangeError : Invalid string length

so i have just started to learn javascript, literally had 2 classes on it. So my knowledge is very very limited. But I am trying to make an appointment application and i keep receiving a Uncaught rangeError :Invalid string length error, and i have no idea why or how to fix it. Basically I have been given some code to copy without much explanation to it, so if anyone can help me with this error it would be greatly appreciated. The code where the error is appearing is below and i believe it is the line table += appt.tableRow(); which is causing the issue. There is obviously more to this code, but not sure if it needs to be given, as the issue is in the showTable function
Edit : I just added the whole javascript code to make it easier
var Appointment = function(subject, description,date, time) {
this.subject = subject;
this.description = description;
this.datetime = new Date(date + " " + time);
this.completed = false;
};
Appointment.prototype.isDue = function(){
var now = new Date();
if(this.datetime > now){
return false;
} else {
return true;
}
};
Appointment.prototype.whenDue = function(){
return this.datetime - new Date();
}
Appointment.prototype.toString = function(){
var s = this.subject +'\n'+
this.datetime.toString() + '\n';
if(this.completed){
s +="Not Completed\n\n";
}
return s
};
Appointment.prototype.howManyDaysTill = function() {
var ms = (this.datetime - new Date()) / 24/60/60/1000
return ms;
};
Appointment.prototype.howManyHoursTill = function () {
var hours = (this.datetime - new Date()) /60/60/1000
return hours;
};
Appointment.prototype.getDate = function() {
return this.datetime.toDateString();
};
Appointment.prototype.getTime = function (){
return (this.datetime.getHours()) + ":" + (this.datetime.getMinutes());
};
Appointment.prototype.tableRow = function(){
var tr = "<tr><td>" + this.getDate() + "</td><td>" +
this.getTime() + "</td><td>" + this.subject +
"</td></tr>";
return tr;
};
var appointments = [];
window.onload = function(){
var newButton = document.getElementById("new");
newButton.onclick = function () {
var subj = prompt("Enter a subject title for the appointment");
var desc = prompt("Enter a description for the appointment");
var date = prompt("Enter the appointment date in the format (e.g) 'Sep
25, 2012");
var time = prompt("Enter the appointment time in the format hh:mm");
var a = new Appointment((subj,desc,date,time));
appointments.push(a);
return showTable();
};
var showTable = function() {
var tableDiv = document.getElementById("table"),
table = "<table border='1'>" +
"<thead><th>Date</th><th>Time</th><th>Subject</th><th>Completed</th>
</thead>";
for (var i = 0, j = appointments.length; i < j; j++) {
var appt = appointments[i];
table += appt.tableRow();
}
table += "</table>";
tableDiv.innerHTML = table;
};
}
HTML5
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="appointments.js"></script>
<title>Appointments</title>
</head>
<body>
<button id="new">New Appointment</button>
<div id ="table"></div>
<header>
<h1>
Appointments Book
</h1>
<p> Enter appointment details and press OK to add, Cancel to revert.</p>
</header>
<table>
<tr>
<td>Subject : </td> <td>input type="text" size="40" id="subject"</td>
</tr>
<tr>
<td>Description</td>
<td>
<textarea rows = "5" cols=""50" maxlength="200" id="description">
</textarea>
</td>
</tr>
<tr> <td>Due Date:</td><td><input type ="date" id="duedate"/></td>
</tr>
</table>
<button id = "OK">OK </button><button id = "cancel">Cancel</button>
<hr/>
</body>
</html>
The for loop must increment on i and not on j. The current one is causing a infinite looping and hence the following line is creating a string that is too big to handle by the JS engine and hence the error
table += appt.tableRow();

Increment Serial Numbers and calculate - javascript

Our company has a client that requires a specific checksum on their bar codes. I've come up with the following which allows the user to enter the distributor part number, our part number and our serial number. Right now, when you click save, it will calculate correctly for the bar code with the checksum. NOW, we have added a quantity box so that we can print x number of barcodes with our serial number incrementing by 1. (i.e. 1st serial number is 000001, next will be 000002 and so on). What I've been trying to do for the last week is alter this code so the user can input the part numbers and sn, and it will calculate each bar code up to the quantity.
This is what we would like to have:
If user enters part numbers, first serial number and quantity then clicks "Save" this should be the result. At present, this can only be achieved by entering each serial number and clicking save
*note: while the quantity can be entered, the actual value has not been used in my code...yet
So, to achieve this, I need to find a way to increment the serial numbers without dropping off the leading zeros but maintaining the length of qty.length.
I also need to figure out how to loop each new serial number through with part numbers to get the correct checksum for the bar code. After a week of staring at this, I'm hoping some fresh and experienced eyes can assist. Here is my code.
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Capture Form Fields to CSV</title>
<script type="text/javascript">
<!--
function saveValues() {
var frm = document.form1;
var str = frm.text1.value + frm.text2.value + frm.text3.value;
var dpn = frm.text1.value;
var wpn = frm.text2.value;
var wsn = frm.text3.value;
var strArray = str.split("");
var calcArray = strArray;
var total =0;
for (var i = 0; i < str.length; i++)
strArray[i] = strArray[i].charCodeAt(0);
for (var i = 0; i < strArray.length; i++){
if (strArray[i] >= 65 && strArray[i] <= 90){
calcArray[i] = (strArray[i] - 64) * (i+1)
}
else if (strArray[i] >=97 && strArray[i] <=122) {
calcArray[i] = (strArray[i] - 96) * (i+1)
}
else if (strArray[i] >=48 && strArray[i] <=57) {
calcArray[i] = (strArray[i] - 48) * (i+1)
}
else {
calcArray[i] = 1 * (i+1)
}
}
for (var i in calcArray){
total += calcArray[i];}
var mod2 = str.length - (2*(Math.floor(str.length/2)));
var mod10 = (total + mod2) - (10*(Math.floor((total + mod2)/10))) ;
var chk = mod10;
var record = ""
+ dpn + "," + wpn + "," + wsn + "," +dpn + "~" + wpn + "~" + wsn + "~" + chk + "\n";
frm.textarea1.value += record;
}
function clearText() {
document.form1.textarea1.value = "";P
form1.text1.value = "";
form1.text2.value = "";
form1.text3.value = "";
}
function csvSave() {
var a = document.createElement('a');
with (a) {
href='data:text/csv;base64,' + btoa(document.getElementById('textarea1').value);
download='csvfile.csv';
}
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
//-->
</script>
</head>
<body>
<h1>Capture Form Fields to CSV</h1>
<form name="form1" >
<p>
Distributor Part Number: <input name="text1" type="text" value="GDM1301" /><br />
Our Part Number: <input name="text2" type="text" value="PCBDM160"/><br />
Our Serial Number: <input name="text3" type="text" value="000001"/><br />
Label Quantity: <input name="qty" type="text" value="3"/>
</p>
<p>
<input name="save" type="button" value="Save"
onclick="saveValues(); return false"/>
 
<input name="clear" type="button" value="Clear"
onclick="clearText(); return false"/>
<button onclick="csvSave()">CSV</button>
</p>
<p>
<i>Click 'Save' to add content</i><br />
<textarea id="textarea1" cols="80" rows="20"></textarea>
</p>
</form>
</body>
</html>

Categories

Resources