Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Comparing two text fields

Posted on 2001-08-01
4
Medium Priority
?
167 Views
Last Modified: 2012-05-04
I am comparing two text fields and the second field value has to behigher than the first field value.The code is as under:-

<html>
<head>
     <title>Untitled</title>
     <script language="JavaScript">
     function fixPctEnd(tf, perst)
     {                      
                if((tf.value < perst)&(perst !=0))
                {                      
                        alert('Please enter percentage that is same as or higher than '+perst);
                        tf.value = "";
                        tf.focus();
                }        
     }
     </script>
</head>

<body>
<form>
<input type="text" size="5" name="base" value="">% and
<input type="text" size="5" name="max" value="" onChange="fixPctEnd(this,form.base.value)">%
</form>
</body>
</html>

WHen I enter single digit or double digits in both the fields then, it works fine. I try 12 in the first and 2 in the second, it does not give the alert ??? where am I going wrong......need help. Also, when enter 1 instead of 2 in the second field(with first field as 12) it works.

Thanks, Ajay
0
Comment
Question by:ajay1861
[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
  • 3
4 Comments
 
LVL 15

Expert Comment

by:a.marsh
ID: 6341795
Try this:

<html>
<head>
    <title>Untitled</title>
    <script language="JavaScript">
    function fixPctEnd(tf, perst)
    {                      
               if((tf.value < perst) && (perst !=0))
               {                      
                       alert('Please enter percentage that is same as or higher than ' + perst);
                       tf.value = "";
                       tf.focus();
               }        
    }
    </script>
</head>

<body>
<form>
<input type="text" size="5" name="base" value="">% and
<input type="text" size="5" name="max" value="" onChange="fixPctEnd(this, parseInt(this.form.base.value))">%
</form>
</body>
</html>


Ant
0
 
LVL 15

Accepted Solution

by:
a.marsh earned 200 total points
ID: 6341803
Basically you need to convert the string to an int in order to use it in the way you want.

In fact I've just noticed a mistake.....try this:

<html>
<head>
    <title>Untitled</title>
    <script language="JavaScript">
    function fixPctEnd(tf, perst)
    {                      
               if((parseInt(tf.value) < perst) && (perst !=0))
               {                      
                       alert('Please enter percentage that is same as or higher than ' + perst);
                       tf.value = "";
                       tf.focus();
               }        
    }
    </script>
</head>

<body>
<form>
<input type="text" size="5" name="base" value="">% and
<input type="text" size="5" name="max" value="" onChange="fixPctEnd(this, parseInt(this.form.base.value))">%
</form>
</body>
</html>


Ant
0
 

Author Comment

by:ajay1861
ID: 6342000
Got it. Thanks for your help. Ajay
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6342041
My pleasure.

:o)

Ant
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…

604 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