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
pvg1975Asked:
Who is Participating?
 
ProculopsisConnect With a Mentor Commented:

Is this any closer:

<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=8" />
<title>http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q__26845659.html</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script> 

jQuery(document).ready(function(){

  $("#form1>input").keyup( eventKeyUp );

});

function eventKeyUp() {
  var upperValue = ( $(this).prev().length == 1 )? $(this).prev().val() : Number.MAX_VALUE;
  var value = $(this).val().replace( /^\D*(\d*(?:\.\d*)?)\D*$/, "$1" );
  if ( parseFloat( value ) > upperValue ) value = upperValue;
  if ( value != $(this).val() ) $(this).val( value );
}

</script>
</head>
<body>

<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>

</body>
</html>

Open in new window

0
 
InsoftserviceCommented:
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">
0
 
pvg1975Author Commented:
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
0
 
InsoftserviceCommented:
hi,
try this one i have not checked it properly pls have a look at it.

index.html
jquery-isnumeric.js
0
 
pvg1975Author Commented:
Hi Proculopsis!

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

Thanks again Proc!

Paula
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.