Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 08-22-2004, 12:46 PM   #1 (permalink)
creed
Registered User
 
Join Date: Aug 2004
Posts: 23
creed is on a distinguished road
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 10:15 AM.
creed is offline   Reply With Quote
Old 08-22-2004, 04:17 PM   #2 (permalink)
DavH27
PHP Pilgrim
 
DavH27's Avatar
 
Join Date: Aug 2004
Location: London
Posts: 170
DavH27 is on a distinguished road
Now what possible practical use can this be?
__________________
Davy - Programming since 1998 [CV]
Currently working on: n/a
Status: n/a
DavH27 is offline   Reply With Quote
Old 08-22-2004, 04:21 PM   #3 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,175
Belisarius is on a distinguished road
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?
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-22-2004, 04:22 PM   #4 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,175
Belisarius is on a distinguished road
He could be doing checksums, although MD5 would be a much better way of doing it.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-22-2004, 05:14 PM   #5 (permalink)
creed
Registered User
 
Join Date: Aug 2004
Posts: 23
creed is on a distinguished road
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.
creed is offline   Reply With Quote
Old 08-23-2004, 06:26 AM   #6 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,175
Belisarius is on a distinguished road
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.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-23-2004, 07:40 AM   #7 (permalink)
creed
Registered User
 
Join Date: Aug 2004
Posts: 23
creed is on a distinguished road
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.
creed is offline   Reply With Quote
Old 08-23-2004, 09:46 AM   #8 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,175
Belisarius is on a distinguished road
You can try to concatinate a blank string to it.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-23-2004, 10:11 AM   #9 (permalink)
creed
Registered User
 
Join Date: Aug 2004
Posts: 23
creed is on a distinguished road
Actually that worked, in a manner of speaking.

String(fArrayResult).charAt(0);

tried this, and poof, problem solved

Thanks all for the replies
creed is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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 06:06 PM
Using javascript to submit a form sammy HTML, XML, Javascript, AJAX 2 07-15-2004 02:31 PM
javascript alert without refreshing? sde HTML, XML, Javascript, AJAX 2 06-30-2004 12:52 PM
javascript function help.. Admin HTML, XML, Javascript, AJAX 2 03-09-2003 07:35 PM


All times are GMT -8. The time now is 12:53 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting