Link to home
Start Free TrialLog in
Avatar of pvg1975
pvg1975Flag for Argentina

asked on

Javascript to check numeric fields

Hello there everybody :)

Im a Javascript newbee and I am really stuck with something. I hope you guys can help me out!

<form id="form1" name="form1" method="post" action="">
Commission:
    <input name="COMISSION" type="text" id="COMISSION" value="70" />
Referral 1:
    <input type="text" name="R1" id="R1" />
Referral 2:
    <input type="text" name="R2" id="R2" />
Sales Manager:
    <input type="text" name="S1" id="S1" />
Sales Agent:
    <input type="text" name="S2" id="S2" />
</form>

Is it possible with Javascript to allow only numbers to be entered on the textbox, and that those numbers cannot be bigger than the previous one?

For example, the field COMMISSION has a value of 70, so on R1 I can put any number below that, there is a catch, if I set R1 to lets say 20, R2 cannot be bigger than 50 (70-20), and if I enter 15 at R2, S1 cannot be bigger than 35 (70-20-15)

I will appreciate if you guy can help me

Thanks!

Paula
Avatar of Insoftservice inso
Insoftservice inso
Flag of India image

hi,

function isNumberKey(e) {
    var k;
    document.all ? k = e.keyCode : k = e.which
    var numeric =  ((k > 45 && k < 58) || k == 8 || k == 9 || k == 0 || k == 44); // 44=, 46=.
      if(numeric == true)
      {
            return ((k > 45 && k < 58) || k == 8 || k == 9 || k == 0 || k == 44);
      }
      else
      {
            alert(NOT_A_NUMBER)
            return ((k > 45 && k < 58) || k == 8 || k == 9 || k == 0 || k == 44);
      }
}

<input type="text" onkeypress="return isNumberKey(event)"  name="data">
Avatar of pvg1975

ASKER

THANKS INSOFTSERVICE!

I tried your scipt, but I can enter a number like "56.65.36". The script allows me to enter more than one decimal point, which brings me trouble for the math
hi,
try this one i have not checked it properly pls have a look at it.

index.html
jquery-isnumeric.js
ASKER CERTIFIED SOLUTION
Avatar of Proculopsis
Proculopsis

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pvg1975

ASKER

Hi Proculopsis!

YEs, that exactly what I need! Wow Javascript is SOO difficult. I will try to understand the lines.

Thanks again Proc!

Paula