homepage Welcome to WebmasterWorld Guest from 54.234.74.85
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
hours verification problem
LinuxGold




msg:1480116
 3:57 pm on Nov 2, 2005 (gmt 0)

I've been slaving on javascript problem for a long while since I'm a newbie on javascript. I'm ASP/SQL application developer trying to reduce the traffic between ASP and SQL. What I'm trying to do in this situation is to verify if kpto is more than 0, then ensure that kpto+khrs is less than 8, otherwise return false. Can anyone catch my error here?

Thanks In Advance,

Scott


function validateKronos()
{
digits="0123456789."
if(krono_process.assocID.options[0].selected)
{
alert("Associate is not selected")
krono_process.assocID.focus()
return false;
}
else
{
var kph=krono_process.khrs.value
len=kph.length
if(len==0)
{
alert("Hours is not indicated. Enter 0 if no hours worked.")
krono_process.khrs.focus()
return false;
}
else
{
for(i=0;i<len;i++)
{
if(digits.indexOf(kph.charAt(i))<0)
{
alert("Hours must be numeric")
krono_process.khrs.focus()
return false;
}
}
var kpp=krono_process.kpto.value
len=kpp.length
if(len!=0)
{
for(i=0;i<len;i++)
{
if(digits.indexOf(kpp.charAt(i))<0)
{
alert("PTO must be numeric")
krono_process.kpto.focus()
return false;
}
}
var kptot=krono_process.kpto.value
var khtot=krono_process.khrs.value
ktot=kpp+kph
If(ktot>8)
{
alert("Exceeding 8 hours total to use PTO")
krono_process.khrs.focus()
return false;
}
}
}
}
}

 

jalarie




msg:1480117
 4:37 pm on Nov 2, 2005 (gmt 0)

I believe the problem is at the line "ktot=kpp+kph;" which is concatinating rather than adding the values. Changing it to "ktot=kpp*1+kph*1;" might help.

Fotiman




msg:1480118
 5:09 pm on Nov 2, 2005 (gmt 0)

You could also try using parseFloat.

kpp = parseFloat(kpp);
kph = parseFloat(kph);
if(!isNaN(kpp) &&!isNaN(kph) )
{
ktot = kpp + kph;
}

Bernard Marx




msg:1480119
 5:16 pm on Nov 2, 2005 (gmt 0)

[b][red]I[/red][/b]f(ktot>8)

These might be useful too (place outside function):

/* change corrupted char to a pipe */
String.prototype.trim = function(){ return this.replace(/^\s+
\s+$/g,'')}
String.prototype.isNumeric = function(){ return this.test(/^[0-9\.]+$/);}

LinuxGold




msg:1480120
 6:10 pm on Nov 2, 2005 (gmt 0)

Ok, here is the latest source so far, I was able to bring up "passed
here, with value of 12." (6 kph and 6 kpp) but didn't bring up the
next alert "Exceeding 8 hours total to use PTO" it should happen.

kpp = parseFloat(kpp);
kph = parseFloat(kph);
if(!isNaN(kpp) &&!isNaN(kph) )
{
ktot = kpp + kph;
}
alert("passed here, with value of " + ktot + ".")
If(ktot>'8')
{
alert("Exceeding 8 hours total to use PTO")
krono_process.khrs.focus()
return false;
}

Fotiman




msg:1480121
 6:21 pm on Nov 2, 2005 (gmt 0)


If(ktot>'8')

You have an uppercase 'I' in 'If'. Also, ktot contains a number now, but you're comparing it to the string '8'. Try this:


if(ktot > 8)

That should do it.

LinuxGold




msg:1480122
 6:42 pm on Nov 2, 2005 (gmt 0)

yes!

that was the problem all along -- my apologies :(

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved