Solved

Comparing two text fields

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

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…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

792 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