Solved

Comparing two text fields

Posted on 2001-08-01
4
158 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
  • 3
4 Comments
 
LVL 15

Expert Comment

by:a.marsh
Comment Utility
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 50 total points
Comment Utility
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
Comment Utility
Got it. Thanks for your help. Ajay
0
 
LVL 15

Expert Comment

by:a.marsh
Comment Utility
My pleasure.

:o)

Ant
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

744 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

11 Experts available now in Live!

Get 1:1 Help Now