• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 682
  • Last Modified:

Javascript condition

Hello Experts,

I have a simple JS function, where I have two text boxes txt1 and txt2 in ASPX page, where I check the value entered(always numeric in both text boxes) in txt2  be greater or lesser on OnChange() event of txt2.The event fires all the time.
function Check(nextval)
        {
             var curr = document.getElementById("tx1").value;
              
              if(nextval< curr) 
              {
                 document.getElementById("txt2").value = '';
                 alert("Please enter greater than first.");
              }     
        }

Open in new window

Sometimes, my condition breaks and get the alert if my nextval is greater than curr.I did my testing with some alert boxes checking the value of both the textboxes.All the time it showed the value entered.

And also I need the focus on txt2 if my above condition is true.

Thanks,
ASPDEV
0
ASPDEV
Asked:
ASPDEV
  • 3
  • 3
1 Solution
 
Dave BaldwinFixer of ProblemsCommented:
How are you calling the function?  You need two statements to get the values of the text boxes.
var curr = document.getElementById("tx1").value;
var curr2 = document.getElementById("tx2").value;

Open in new window

In addition, if they can be more than a 1 digit number, you have to convert them to numbers instead of strings.  The easiest way is usually to multiply the value by 1 which is enough to tell javascript to handle them as numbers.
0
 
ASPDEVAuthor Commented:
Worked.

Thanks,
ASPDEV
0
 
ASPDEVAuthor Commented:
How can I get focus for txt2 on condition true.
0
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.

 
Dave BaldwinFixer of ProblemsCommented:
document.getElementById('txt2').focus()
0
 
ASPDEVAuthor Commented:
I tried already, this doesn't work. Also, tried removing the alert msg box,no luck.

Any other way around,
0
 
Dave BaldwinFixer of ProblemsCommented:
Here is a demo.  I could only get the 'focus()' to work thru using the submit button.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>JS onfocus test</title>
<script type="text/javascript">
<!--
function Check()
	{
	var curr = document.getElementById("txt1").value;
	var curr2 = document.getElementById("txt2").value;
	//var curr = document.forms.form1.txt1.value;
	//var curr2 = document.forms.form1.txt2.value;
	if(curr2 < curr) {
		alert("curr2 is less than curr.");
		//document.forms.form1.txt2.focus();
		document.getElementById("txt2").focus();
		return false;
		}
	}

// -->
</script>
</head>
<body>
<h1>JS onfocus test</h1>
<form name="form1" action="#" method="get" onsubmit="return Check()">
<input type="text" name="txt1" id="txt1" /><br>
<input type="text" name="txt2" id="txt2" /><br>
<input type="submit" name="submit" id="submit" value="submit" />
</form>
</body>
</html>

Open in new window

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now