?
Solved

Comparing two text fields

Posted on 2001-08-01
4
Medium Priority
?
165 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Suggested Courses

801 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