|
 |
|
 |
08-22-2004, 11:46 AM
|
#1 (permalink)
|
|
Registered User
Join Date: Aug 2004
Posts: 23
|
Issues with Javascript
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:
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[i] * 2;
if (fArrayResult > 9) {
firstNum = fArrayResult.charAt(0);
secondNum = fArrayResult.charAt(1);
secondArray[i] = firstNum + secondNum;
}
else {secondArray[i] = 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
Last edited by creed; 08-23-2004 at 09:15 AM.
|
|
|
08-22-2004, 03:17 PM
|
#2 (permalink)
|
|
PHP Pilgrim
Join Date: Aug 2004
Location: London
Posts: 170
|
Now what possible practical use can this be?
__________________
Davy - Programming since 1998 [CV]
Currently working on: n/a
Status: n/a
|
|
|
08-22-2004, 03:21 PM
|
#3 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,166
|
A) What do you mean it doesn't work?
B) Have you used Mozilla's Javascript debuggin tools?
C) Have you isolated where it breaks down?
|
|
|
08-22-2004, 03:22 PM
|
#4 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,166
|
He could be doing checksums, although MD5 would be a much better way of doing it.
|
|
|
08-22-2004, 04:14 PM
|
#5 (permalink)
|
|
Registered User
Join Date: Aug 2004
Posts: 23
|
Quote:
|
A) What do you mean it doesn't work?
|
That might be helpful woudlnt it  sorry bout that.
Right now it will get to as far as the following section:
Code:
for (var i = 0; i <= 4; i++) {
fArrayResult = firstArray[i] * 2;
if (fArrayResult.value>10) {
firstNum = fArrayResult.charAt(0);
secondNum = fArrayResult.charAt(1);
secondArray[i] = firstNum + secondNum;
}
else {secondArray[i] = fArrayResult;};
Here it will continue fine if the value is less than 10. Hwoever, if the value is 10 or greater, it will give an error of "Object doesnt support that property of method", referring to fArrayResult.charAt.
I probably have something defined incorrectly, but at this moment, I'm at a loss as to what it is.
|
|
|
08-23-2004, 05:26 AM
|
#6 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,166
|
I think you're running into the problems of a weakly-typed language. The Javascript interpreter is probably having problems switching between the Integer type, and the String type. Note you declare
Code:
fArrayResult = firstArray[i] * 2;
This causes Javascript to consider the result to be an int. You also say
Code:
if (fArrayResult.value>10) {
But then you say
Code:
firstNum = fArrayResult.charAt(0);
That's an operation on a String object. But, because Javascript sometimes lets you move between the two without ever realizing it, confusion results. This is why I hate weakly typed languages. The error only occurs in values of greater than 10 because the charAt method is only called when the value is greater than ten.
|
|
|
08-23-2004, 06:40 AM
|
#7 (permalink)
|
|
Registered User
Join Date: Aug 2004
Posts: 23
|
Well that's a pain. One reason why I never bothered with Javascript too often. Is there a way to parse numbers other than using charAt at all? Pretty much all i'm after is a way to split the number in two and then add them together (for example if the number was say, 18, it would split it half, making firstNum=1 and secondNum=8, and then add the two variables together, which woudl equal 9.
I'm sure there has to be a way to do it, unfortuenatly tryign to find out how seems to be much harder to find than with other languages such as ASP, PHP and Java.
And if it helps anyone, the error that I get is that fArrayResult.charAt is not a function, which kinda baffles me.
|
|
|
08-23-2004, 08:46 AM
|
#8 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,166
|
You can try to concatinate a blank string to it.
|
|
|
08-23-2004, 09:11 AM
|
#9 (permalink)
|
|
Registered User
Join Date: Aug 2004
Posts: 23
|
Actually that worked, in a manner of speaking.
String(fArrayResult).charAt(0);
tried this, and poof, problem solved
Thanks all for the replies 
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
|
Small modification needed on image flipping JavaScript
|
Epsilon |
HTML, XML, Javascript, AJAX |
0 |
08-31-2004 05:06 PM |
|
Using javascript to submit a form
|
sammy |
HTML, XML, Javascript, AJAX |
2 |
07-15-2004 01:31 PM |
|
javascript alert without refreshing?
|
sde |
HTML, XML, Javascript, AJAX |
2 |
06-30-2004 11:52 AM |
|
javascript function help..
|
Admin |
HTML, XML, Javascript, AJAX |
2 |
03-09-2003 06:35 PM |
All times are GMT -8. The time now is 10:01 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|