Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script help!

Status
Not open for further replies.

Deleted

Technical User
Jul 17, 2003
470
US
Hello all,

I have written this javascript below. What the script does is checks the quantity entered by the user (on the fly) and compares the input to the increments required.

1 thru 47 *must order increments of 2
48 - 96 *must order increments of 48
96++ *must order in increments of 96

Everything works fine with exception to numbers 1 - 9.

The problem is that when entering, let's say 33, the first digit gets read and prompts 'increments of 2' instead of allowing the full number to be processed.

Any ideas? Thanks in advance..

Code:
<html>
<head>

<script language="javascript">
<!--//
var input;
var qty;

function getQuantity(number) {
	input = number;
	checkQuantity();
}

function checkQuantity() {
	qty = input.value;

	if (qty < 48) {
		if (qty%2 != 0 && qty.length >= 2) {
			alert('Select quantity in increments of 2');
			document.this_form.quantity.value = "";
		}
	}
	else
	if (qty >= 48 && qty < 96) {
		if (qty%48 != 0) {
			alert('Select quantity in increments of 48');
			document.this_form.quantity.value = "";
		}
	}
	else
	if (qty >= 96) {
		if (qty%96 != 0) {
			alert('Select quantity in increments of 96');
			document.this_form.quantity.value = "";
		}
	}

	setTimeout(checkQuantity, 100);
}
//-->
</script>

<title>My Script</title>
</head>
<body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0" marginwidth="5" marginheight="0">

<br>
<form method="POST" name="this_form">

INPUT:  <input type="text" name="quantity" size="40" onClick="getQuantity(this)">

<input type="submit" value="Select">
</form>
<br>

</body>
</html>

 
Just check the length of input value, if it's less 2, then add 0 infront of it.
 
Try to replace
Code:
if (qty < 48) {
  if (qty%2 != 0 && qty.length >= 2) {
    alert('Select quantity in increments of 2');
    document.this_form.quantity.value = "";
  }
}
by
Code:
if (qty < 48) {
  if (qty%2 != 0) {
    alert('Select quantity in increments of 2');
    document.this_form.quantity.value = "";
  }
}

and set timeout for a longer time, such as 2000
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top