All Things Techie With Huge, Unstructured, Intuitive Leaps

Javascript -- Round Number to nearest 100 -- Validate

I have this web page that is created by a jsp.  The user has to input an amount of money to make a bid, but I don't want the users entering silly amounts like $1098.66.  I want the amount to be in exact increments of $100.  So I have an input tag "<input tabindex='1' type='text' name='amt' id='amt' value='00.00'  size='10' />" and I want to validate and make sure that the number is an increment of $100.  When the user presses the submit button, I call onclick="javascript:validateAndSubmit()".

My validate and submit function is something like this:


var bestPrice = document.(insert Form Name).amt.value;
                //get rid of the dollar sign
var match= bestPrice.match(/[0-9,.]*/);
if (match!==null) {
                    //Make the number into a float
   var amount= parseFloat( match[0].replace(/,/g, '') ); 
                 //find out if the number is not in multiples of 100 using modulus
   var rem = amount % 100;
   if ( rem > 0)
    {
    alert("Your offer must be in multiples of $100.")
    }

Hope that this helps.

No comments:

Post a Comment