creed
Posts: 1
|
| Posted: 08/22/2004, 9:46 AM |
|
Hey all
I've been racking my brain tryign to figure out why this not only doesnt work, but doesnt even return an error message in any web browser that I've tried. Here's the code:
<html>
<head>
<script language="JavaScript"><!--
//defines input as an integer
function isInteger(value) {
return (parseInt(value) == value);
}
var integer = /^\d+$/;
function validate(form) {
//validates the input as a nine digit integer, all else is denied
var stockNumber = '';
var output = '';
if (!isInteger(form.myField.value)) {
output = 'Entry must be a number\n';
alert(output);
form.myField.focus();
return(false);
}
if (form.myField.value.length!=9) {
output = 'Entry must be nine digits long\n';
alert(output);
form.myField.focus();
return(false);
}
stockNumber = form.myField.value;
//collect numbers needed for operation into arrays
var firstArray = new Array(5);
var secondArray = new Array(5);
var thirdArray = new Array(4);
firstArray[0] = stockNumber.charAt(0);
firstArray[1] = stockNumber.charAt(2);
firstArray[2] = stockNumber.charAt(4);
firstArray[3] = stockNumber.charAt(6);
firstArray[4] = stockNumber.charAt(7);
thirdArray[0] = stockNumber.charAt(1);
thirdArray[1] = stockNumber.charAt(3);
thirdArray[2] = stockNumber.charAt(5);
thirdArray[3] = stockNumber.charAt(7);
fArrayResult = 0;
arrayTotal = 0;
//if fArrayResult is larger than 9, parse the two numbers, add together, and store in secondArray, otherwise, just store in second array
for (var i = 0; i <= 4; i++) {
fArrayResult = firstArray * 2;
if (fArrayResult > 9) {
firstNum = fArrayResult.charAt(0);
secondNum = fArrayResult.charAt(1);
secondArray = firstNum + secondNum;
}
else {secondArray = fArrayResult;};
}
//add all numbers together from both second and third array
total = secondArray[0] + secondArray[1] + secondArray[2] + secondArray[3] + secondArray[4] + thirdArray[0] + thirdArray[1] + thirdArray[2] + thirdArray[3];
//if the total is either 0, or the second number is 0, define check digit as 0, otherwise check digit is 10-second digit
if (total == 0) {
alert('Check digit is 0');
}
else if (total.charAt(1) == 0) {
alert('Check digit is 0;');
}
else { newTotal = 10 - total.charAt(1);
alert(newTotal);
}
}
//--></script>
</head>
<body>
<form onsubmit="return validate(this)">
<input type="text" name="myField">
<input type="submit" value="Submit">
</form>
</body>
</html>
/
As you can see it's supposed to take a nine digit number, validate it, and find a certain combination based on the operations that i've tried to specify. for example, if you typed in 987654321, it shoudl return 7.
Any help on what's going wrong ehre would be appriciated.
|