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

Javascript check value of another field

Need a working example of javascript checking the value of one field when the value of another field is selected.

For example:
On change of list1, and only when list1 select is set to value "1" for active, check to see if text1 is null. If null, send alert. If not null, do nothing.

Follow this form as an example please.

<form action="submit.asp" name=method="get">
<input name="text1" type="text">
<br><br>
<select name="list1">
  <option selected>Select...</option>
  <option value="1">Active</option>
  <option value="2">Test</option>
</select>
<br><br>
<input name="" type="submit" value="Submit">
</form>
0
bradylanter
Asked:
bradylanter
3 Solutions
 
hernst42Commented:
Try:

<script>
function valform(f)
{
    if (f.list1.value =="1" && f.text1.value != "") {
        alert('Text must be empty');
        return false;
    }
    return true;
}
</script>
<form action="submit.asp" name=method="get" onsubmit="return valform(this)">
<input name="text1" type="text">
<br><br>
<select name="list1">
  <option selected>Select...</option>
  <option value="1">Active</option>
  <option value="2">Test</option>
</select>
<br><br>
<input name="" type="submit" value="Submit">
</form>
0
 
BogoJokerCommented:
hernest42's if code is perfect but the logic is slightly off.  You said if the textfield is NULL then send an alert, so the if statement should be:

f.text1.value == ""


- Joe P
0
 
LocoTechCJCommented:
Ok, so I believe you wanted it to check when the selet box changed.... and only if the text box WAS empty when "1" was selected, did you get an alert.  Try this instead:

<script>
function checkSelect()
{
    if ((document.form1.list1.value == "1") & (document.form1.text1.value == "")) {
        alert('Textbox 1 must be completed');
    }
}
</script>
<form name="form1" action="submit.asp" method="get">
<input name="text1" type="text">
<br><br>
<select name="list1" onchange="checkSelect()">
  <option selected>Select...</option>
  <option value="1">Active</option>
  <option value="2">Test</option>
</select>
<br><br>
<input name="" type="submit" value="Submit">
</form>

Hope this helps,
LTCJ
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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