Link to home
Start Free TrialLog in
Avatar of ASPDEV
ASPDEV

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ASPDEV
ASPDEV

ASKER

Worked.

Thanks,
ASPDEV
Avatar of ASPDEV

ASKER

How can I get focus for txt2 on condition true.
document.getElementById('txt2').focus()
Avatar of ASPDEV

ASKER

I tried already, this doesn't work. Also, tried removing the alert msg box,no luck.

Any other way around,
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