Solved

Javascript to check numeric fields

Posted on 2011-03-12
5
284 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
  • 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now