Click to See Complete Forum and Search --> : Calculate Ranges in text box


mexavin
11-07-2003, 06:28 AM
I currently have it set up to count the number of numbers in the 4 text boxex, then add them up. Now, instead, I need to be able to count ranges of numbers, and add them up. EXAMPLE: The user can input 100-105, 107, 109, 111-115.
The box should calculate this as = 13. Not sure how to do this. Any help will be greatly appreciated. Here is what I have so far:
===============
<html>
<head>
<script language="javascript">
function cnt1(w,x){
var y=w.value;
var r = 0;
a=y.replace('\n',' ');
a=a.split(' ');
for (z=0; z<a.length; z++) {if (a[z].length > 0) r++;}
x.value=r;
}
</script>
<script>
function calculate()
{
document.calcform.quantity.value = (eval(document.calcform.qty1.value) + eval(document.calcform.qty2.value) + eval(document.calcform.qty3.value) + eval(document.calcform.qty4.value))
}
</script>
</head>
<body bgcolor="#ffffff" >
<form method=post action="../shopaddtocart.asp" name="calcform">
STEP 1: Enter the first set of numbers <br>
Separate each number with a space<br>
(Example: 101 102 103 104)
<input size="100" class=txtfield name="Feature3" onkeyup="cnt1(this,document.calcform.qty1)">
<br>
&nbsp; <br>
STEP 2: Enter Second set of numbers<br>
Separate each number with a space<br>
(Example: 101 102 103 104)
<input size="100" class=txtfield name="Feature4" onkeyup="cnt1(this,document.calcform.qty2)">
<br>
&nbsp; <br>
STEP 3: Enter Third Set of numbers<br>
Separate each number with a space<br>
(Example: 101 102 103 104)
<input size="100" class=txtfield name="Feature5" onkeyup="cnt1(this,document.calcform.qty3)">
<br>
&nbsp; <br>
STEP 4th: Enter the Fourth set of Numbers<br>
Separate each number with a space<br>
(Example: 101 102 103 104)
<input size="100" class=txtfield name="Feature6" onkeyup="cnt1(this,document.calcform.qty4)">
<br>
&nbsp; <br>
STEP 2: Place Order Total First Set:
<input type="text" name="qty1" value="0" size="5" />
<br />
Total Second Set:
<input type="text" name="qty2" value="0" size="5"/>
<br />
Total Third Set:
<input type="text" name="qty3" value="0" size="5"/>
<br />
Total 4th Set:
<input type="text" name="qty4" value="0" size="5"/>
<br>
<input type="button" value="TOTALS" onClick="calculate()">
<input TYPE=TEXT class=txtfield size=2 maxlength=3 name="quantity" value="0" >
<input type=submit class=submitbtn value="Order" name=Order>
<br></p>
</form>
</body>
</html>
==============

Thanks a bunch,

Mex

olerag
11-07-2003, 10:44 AM
What happens if the following are entered??

122-5 OR 5-10-205 OR 100,205,15??
Are signed integers permitted?
What if "alpha-numeric" characters are inserted?

I didn't see any of these in your example but since your
using a "textfield" any of the scenarios provided above
would be possible.

mexavin
11-07-2003, 11:04 AM
No, just numbers. They are ordering Hotel Room Keychains. So, they need to order key tags for many rooms. Not all rooms require key tags. So, if they would order like so:

Rooms
101-105, 107, 109

This would equal
101-105 = 5 keytags
107 = 1 keytag
109 = 1 keytag
-------------------------
TOTAL = 7 keytags.

Does this make sense?

Mex

olerag
11-07-2003, 11:30 AM
What your are saying makes sense but the code you
previously provided did not include any numeric-only
code consequently any/all of the possibilities I previously
provided could occur.

Adding the amount of value scenarios is simple; ensuring
that each scenario is formatted properly is the real trick,
otherwise the "count" function can fail.

mexavin
11-07-2003, 11:43 AM
OK, so if all info is put in by the user correctly, what is the answer? I am not worried if people put the info in wrong, just how to calculate it when it is input correctly.

Thanks,
Mex

olerag
11-07-2003, 11:54 AM
I believe Charles alread provided a complete code example
in your other thread; subject name is "NUMBER CALCULATION".

Of course that code is some wild and crazy stuff.

Charles
11-07-2003, 11:58 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>

<style type="text/css">
<!--
label {display:block; margin:1em 0em}
input {display:block}
-->
</style>

<script type="text/javascript">
<!--
String.prototype.count = function () {
var a = this.match(/\d+(\s*-\s*\d+)?/g);
var n = 0;
for (var j=0; j<a.length; j++) {if (/(\d+)\s*-\s*(\d+)/.test(a[j])) {n += Math.abs (RegExp.$2 - RegExp.$1)}; n++}
return n;
}
// -->
</script>

<form action="">
<div>
<label>Range<input onchange="alert(this.value.count())" type="text"></label>
<button type="submit">Submit</button>
</div>
</form>

Charles
11-07-2003, 12:00 PM
Originally posted by olerag
I believe Charles alread provided a complete code example
in your other thread; subject name is "NUMBER CALCULATION".

Of course that code is some wild and crazy stuff. I believe that I was misunderstanding the question when I made that stab. Give this one a try.

mexavin
11-07-2003, 12:21 PM
Charles,
Your second source was EXACTLY what I am looking for! I greatly appreciate your help.

Thanks again,

Mex