Bust my code!

J

jhaight

I created this order form for my company that will only allow
multiples of 10 copies to be ordered for certain documents. However, I
have had two customers day that when entering their order, they cannot
get the function to work correctly. Please let me know if you can find
a problem with my code and tell me what I'm doing wrong! I've tested
on IE 6.0 and Mozilla 3.0. Please, bust my code!
 
D

dhtml

function rounder(aObj) {
if (aObj.value == "") {
return;
}
aObj.value = Math.ceil(parseInt(aObj.value) / 10) * 10;
}


Maybe:

function shiftValueUpTo10(input) {
var val = parseInt(input.value, 10);

// Fix bug for NaN, 0, et c.
if(!val) {
input.value = "";
} else {
input.value = Math.ceil(val/10) * 10;
}
}

Use parseInt with a radix to avoid mishaps with base 8 (octal).

For example, the user might have entered: "08"

That results in the base 8 being used and the result of:
parseInt("08")

- is 0.


Garrett
 
R

RobG

function rounder(aObj) {
   if (aObj.value == "") {
     return;
   }
   aObj.value = Math.ceil(parseInt(aObj.value) / 10) * 10;

}

Maybe:

function shiftValueUpTo10(input) {
   var val = parseInt(input.value, 10);

It seems sensible to validate what the user has entered before using
it, that way parseInt can be ditched entirely, e.g.:

function isInteger(n) {
var re = /^\d+$/;
return re.test(n);
}

function shiftValueUpTo10(input) {
var val = input.value;
input.value = isInteger(val)? ((val/10|0) +1 )*10 : '';
}
 
D

dhtml

RobG said:
It seems sensible to validate what the user has entered before using
it, that way parseInt can be ditched entirely, e.g.:

function isInteger(n) {
var re = /^\d+$/;
return re.test(n);
}

function shiftValueUpTo10(input) {
var val = input.value;
input.value = isInteger(val)? ((val/10|0) +1 )*10 : '';
}

Hmm. If value is "0", then value becomes "10".

The user might see this as an unscrupulous way to add unwanted items to
his cart.
 
R

RobG

Hmm. If value is "0", then value becomes "10".

The user might see this as an unscrupulous way to add unwanted items to
his cart.

Or useful assistance provided by his or her friendly
(over)supplier. :)
 
D

Dr J R Stockton

Use parseInt with a radix to avoid mishaps with base 8 (octal).

IMHO. parseInt and parseFloat should ONLY be used when unary + (or
unary -) would not serve.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top