Welcome to WebmasterWorld Guest from 54.158.51.150

Forum Moderators: open

Message Too Old, No Replies

hours verification problem

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

10+ Year Member



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;
}
}
}
}
}
4:37 pm on Nov 2, 2005 (gmt 0)

10+ Year Member



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.
5:09 pm on Nov 2, 2005 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



You could also try using parseFloat.

kpp = parseFloat(kpp);
kph = parseFloat(kph);
if(!isNaN(kpp) &&!isNaN(kph) )
{
ktot = kpp + kph;
}
5:16 pm on Nov 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[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\.]+$/);}

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

10+ Year Member



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;
}
6:21 pm on Nov 2, 2005 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month




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.

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

10+ Year Member



yes!

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

 

Featured Threads

Hot Threads This Week

Hot Threads This Month