views:

64

answers:

2

How could I change this code to not allow a 0 and to strip out all non number characters?

<script type="text/javascript">
    (function() {
        var a= document.getElementsByName('a')[0];
        var b= document.getElementsByName('b')[0];
        var c= document.getElementsByName('c')[0];

        a.onchange=b.onchange=a.onkeyup=b.onkeyup= function() {
            c.value= Math.ceil((a.value/b.value)*100);
        };
    })();
</script>
+1  A: 
a.onchange=b.onchange=a.onkeyup=b.onkeyup= function() {
  var av = parseFloat(a.value), bv = parseFloat(b.value);
  if(bv != 0)
    c.value= Math.ceil((av/bv)*100);
};
sje397
c.value = bv != 0 ? Math.ceil((av/bv)*100) : "Invalid"; // To clear the filed if error.
some
this is wrong for one main reason. you cannot parseFloat just like that because he wants to validate the data. If he enters a character into value a or b, then an error will occur during runtime as the program will try to parse the nonnumber into a float. which wont work.
Pavan
He didn't say he wants to validate the data - and so it depends on your interpretation of "strip out all non number characters". This method does not produce an error - but can evaluate the input text as 'NaN' if it's not 'parse-able'. It just produces 'NaN' for nonsensical input - effectively ignoring non-numeric input - which is my interpretation of the OP's goal.
sje397
yes you are absolutely right, it does indeed produce an output of 'NaN'. Your code however does not strip out non numerical characters.
Pavan
@Pavan: as I mentioned, that depends on your interpretation of 'strip out'. Admitedly, I've used a rather flexible one ;)
sje397
+1  A: 

EDIT: updated answer:

you simply strip all the non non numbers then test if the number is not a 0 THEN you can perform your function.

   a.onchange=b.onchange=a.onkeyup=b.onkeyup= function() {

    // 1. First we will remove all non numbers from the values inputted by the user.
    var aValue = new String(a.value);
    var bValue = new String(b.value); 

    //Use regular expressions to strip out the non numbers incase the user types in non numbers.
    aValue = aValue.replace(/[^0-9]/g, '');
    bValue = bValue.replace(/[^0-9]/g, '');

    float newAValue = parseFloat("aValue"); 
    float newBValue = parseFloat("bValue"); 

    //2. Then test to see if the user has typed 0 as the value if they haven't then you an perform the maths.

    if((newAValue != 0) && (newBValue != 0))
        c.value= Math.ceil((av/bv)*100);
    };

Hope this helps. Thanks Let me know if it does.

PK

Pavan
1) 'float' produces a syntax error (should be `var`), 2) you're missing an open brace on the 3rd to last line, 3) `av` and `bv` are not defined and should be `aNewValue` and `bNewValue` on the 2nd to last line, 4) it produces 'NaN' if there are only letters in the input
sje397
trying to get this to work but not having much luck so far
James
keeps resulting in "NAN"
James
using a mix of the two it now seems to be working
James
im glad this worked for you. i didnt want to answer straight away incase you were debugging your code. and youve fixed it now so thats great. Thanks. if you could could you write in the comment what you did that would be great.
Pavan