javascript comparing numbers

Hi I have javascript function which doesnot allow the user to enter the amount in a text box if the
amount entered in fundsamount text box is greater than remaining funds. Remaining funds is a text box and is pre populated from database with $ sign in front
like($5000.00).
Alert should be displayed in both cases whether the user enters a $ sign or not in front of the numbers

My problem is that my alert does not display the correct number if the remainingfunds is $888.00
the alert shows 88.0 so if the user enters 800.00 it gives the alert which is wrong since it compares 800 with 80 instead of 800 with 880

The alert should also be displayed if the remainingfunds is a -ve number  diaplyed as ($9,314.24) on screen.


      function IsNumber(e)
    {      
            var num = e.value.replace(/[$]/,"");
            var num = e.value.replace(/[,]/,"");
             num = num.toString().replace(/[^0-9.]/g,'');
            return num;
    }  


function SubFundAmount()
        {
            var Remainingfunds= IsNumber(window.document.getElementById('<%=remainingfunds.ClientID %>'));
            var fundsAmount  = IsNumber(window.document.getElementById('<%=fundsamount.ClientID %>'));
                var Remainingfunds= = parseFloat(RemainingBudget.substr(1))
           
   alert(Remainingfunds)
               
            if fundsAmount  > Remainingfunds)
                {
                       alert("WARNING:  amount cannot be greater than remaining fund.");
                       window.document.getElementById('<%=fundsamount.ClientID %>').focus();
                         window.document.getElementById('<%=fundsamount.ClientID %>').select();
                             return false;
                   }
                  else
                      {
                           
                            window.document.getElementById('<%=notes.ClientID %>').focus();
                            window.document.getElementById('<%=notes.ClientID %>').select();
                            return true;
                      }
           }  



I also have the ffollowing function in my common js file that is included in the page

function ValidNumber( formElem )
      {      
            
            if (formElem.value == "") formElem.value = "0";
            return formElem.value;
      }
                           
PNKJAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
change this:
var Remainingfunds= = parseFloat(RemainingBudget.substr(1))

to:
var Remainingfunds= (RemainingBudget.charAt(0)=="$" ? parseFloat(RemainingBudget.substr(1)) : parseFloat(RemainingBudget);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LordWolfyCommented:
I've only had a quick glance at this but it looks like your IsNumber function is doing a string replace of a number with an enpty string (num = num.toString().replace(/[^0-9.]/g,'');)

Wouldnt that shave off the first character (so 880 becomes 80)?
0
PNKJAuthor Commented:
Tried this but it gives syntax error
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

PNKJAuthor Commented:
var Remainingfunds= (RemainingBudget.charAt(0)=="$" ? parseFloat(RemainingBudget.substr(1)) : parseFloat(RemainingBudget);    This gives error
0
Michel PlungjanIT ExpertCommented:
perhaps just

remove

 var Remainingfunds= = parseFloat(RemainingBudget.substr(1))

I would do this:


function IsNumber(str) {
  if (str.length==0) return 0;      
  var num = str.replace(/[$]/,"");
  var num = str.replace(/[,]/,"");
  return num.toString().replace(/[^0-9.]/g,'');
}  
 
 
 
 
function SubFundAmount() {
  var Remainingfunds= IsNumber(window.document.getElementById('<%=remainingfunds.ClientID %>').value);
  var fundsAmount  = IsNumber(window.document.getElementById('<%=fundsamount.ClientID %>').value);
 
  alert(Remainingfunds)

Open in new window

0
hieloCommented:
>> ...This gives error
Well, where are you getting/setting the value for RemainingBudget? On your code fragment ti is not initialized anywhere. I assume you had it initiazed somewhere. Trying to help as best as I can with the little you posted.
0
PNKJAuthor Commented:
In Both the solution from hielo and mplungjan: alert gets displayed in both conditions whether the amount entered is less or more.
Remaining amount will alwys have dollar sign displayed in front for eg $4500.00
But in fundsamount  in edit mode user can either enter $ sign or not.

I am calling this function onblur event

thanks


0
hieloCommented:
If you would like to be helped, then help US help YOU. What you posted depends on references to fields which or may not exist at all. We can't tell from what you posted. What you need to do is open the page in question through Firefox, Type CTRL+U, copy the source code, and pasted it here.
0
PNKJAuthor Commented:
Please see the attached code on my aspx file.  Amount field should not allow the userto enter only $ with numbers or just numbers
code.txt
0
hieloCommented:
What is "fund_note.ClientID" supposed to be?
0
PNKJAuthor Commented:
it sis the next control if the amount is ok then the focus goes to the next field
0
PNKJAuthor Commented:
Hello all I figured out the problem and by adding
  var SubFundBudget= SubFundBudget.replace(/\$/g,'');

 it works fine for both cases. thanks for all your effort.
0
hieloCommented:
Do you realize that nowhere on your posted code do you have a reference to SubFundBudget. Not Cool on your part. Just wasted precious time.
0
PNKJAuthor Commented:
Sorry but actually it is right the code I sent you was absolutely correct . I changed the names later.
fundsAmount  has been changed to SubFundBudget. The problem in the code was it was comparing $45 with 45 and won't give the alert. looks like replace from IsNumber didn't take care so by adding
var SubFundBudget= SubFundBudget.replace(/\$/g,'');  it worked

var Fundamount  =IsNumber(window.document.getElementById('<%=Fundamount.ClientID %>'));
has been changed to
SubFundBudget =IsNumber(window.document.getElementById('<%=SubFundBudget.ClientID %>'));

I appreciate your time and I did try all your code and will give u somepoint for ur hard work
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.