FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

Javascript function not working!!??

 


mwilson82
I have looked through this for about a day and have not been able to find out how or why this is happening... or a way around it for that matter. Here is what I'm trying to do. I have a form that has a text box and a submit button. The user types in how many credits they want to bet on whatever they are interested in, and then once the button is hit, Javascript kicks in and checks to make sure its only numbers in there and also that they have enough credits available before it actually goes in and processes the bet.
Code:

<SCRIPT LANGUAGE="JavaScript">

function testResults (form) {

TestVar = isNumberString (form.bet.value);

if (TestVar == 1) {
var answer = confirm ("You wish to bet " + form.bet.value + " credits.     Is this correct?");

if (answer) {
if (form.bet.value <= form.credits.value)
window.location=(new_url...works fine);
else
alert("You don't have enough credits to bet that!");
}
}
else
alert ("Please enter only numeric characters.");
}

//This is just to confirm its only numbers... works fine
function isNumberString (InString)  {
if(InString.length==0) return (false);
var RefString="1234567890";
for (Count=0; Count < InString.length; Count++)  {
TempChar= InString.substring (Count, Count+1);
if (RefString.indexOf (TempChar, 0)==-1) 
return (false);
}
return (true);
}

</SCRIPT>


This is what is happening... if I had 10,000 credits available, the max it would allow me to bet is 1,000 credits (seems to cut off one number on the end). So, lets say I put in 5000 into form.bet.value field. It would confirm that I want to bet that much, then if I click OK it would give me an alert saying I don't have enough credits available when, in fact, I do. Now to test the values of the two variables, I actually called the variables in the alert box and the right number of credits is coming up and the correct bet amount is coming up. But still, even though credits equal 10,000 and the bet equals 5,000, the above function of form.bet.value<=form.credits.value is returning false! This doesn't make any sense! I've double checked everything it seems but still Javascript is telling me that 5,000 is not less than 10,000... If I put in a 1,000 credit bet though it will go through and forward me to the new location, but thats the most it will take! I've hit a brick wall here, anyone have any ideas?
hexkid
mwilson82 wrote:
Code:
if (form.bet.value <= form.credits.value)


I don't know JavaScript very well, but I think the values of textboxes are text ... maybe forcing the values to be numbers would help ????

Code:
if ((form.bet.value*1) <= (form.credits.value*1))
mwilson82
Hey, nice one!

That actually worked! Thanks a ton!
venc
wow, impressive, hexkid, you now your stuff.
AftershockVibe
Hexkid is correct but the more elegant way to do this is to use a casting function, in this case parseInt()

Note: I'm assuming you will only be dealing with integers

Code:
if ((form.bet.value*1) <= parseInt(form.credits.value))


When you come back and look at your code again at least it is obvious why you would do this, rather than multiply by one. ParseInt will also be marginally more efficient.

Cool
kawkazEE
you can always consult to google, i often do.. Wink Wink
Reply to topic    Frihost Forum Index -> Scripting -> Html, CSS and Javascript

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.