Solved

Comparing two text fields

Posted on 2001-08-01
4
159 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
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 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

932 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

14 Experts available now in Live!

Get 1:1 Help Now