Solved

Javascript to check numeric fields

Posted on 2011-03-12
5
287 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:pvg1975
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 15

Expert Comment

by:Insoftservice
ID: 35116863
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
 

Author Comment

by:pvg1975
ID: 35116896
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
 
LVL 15

Expert Comment

by:Insoftservice
ID: 35117299
hi,
try this one i have not checked it properly pls have a look at it.

index.html
jquery-isnumeric.js
0
 
LVL 20

Accepted Solution

by:
Proculopsis earned 500 total points
ID: 35120359

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
 

Author Comment

by:pvg1975
ID: 35121741
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Jquery form 9 39
Add a Location and Color Price based on select box selected option 17 65
PHP Web Development 6 20
Two decimal 5 25
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question