Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Validate one form field against another form field

Status
Not open for further replies.

debq

Technical User
Aug 7, 2008
50
US
Hello,

I have a website that includes a form that asks several questions. This information is submitted to a Database. I have two questions that I want to validate against each other so that I do not have to "clean up" the data submitted.

"Were you placed on Hold?" Yes or NO options in a dropdown menu.

If you were placed no Hold, did the person wait for a response before placing you on HOld? Yes or NO or N/A options in a dropdown menu.

I want the person submitting the information to only be allowed to choose "Yes" or " "No" for the second question if and only if they choose "Yes" for the first question.

And

To only be able to choose "N/A" if they choose "No" fo the first question.

I would like the form to not submit and to change the color of the boxes if the answers do not match.

Can I do this with a Javascript check???

Thanks much,
 
This should do it for you. Although, my advice would be to do the checking before the user submit's the form i.e. onChange of the second drop down. It would really annoy me to fill in the form and get back errors after submitting it.

<html>

<head>
<script type="text/javascript">
function check()
{
var onHold = document.getElementById("onHold").value;
var wait = document.getElementById("waitResponse").value;
var changeColor = false;


if((onHold == 'yes' && wait == 'na') || (onHold == 'no' && (wait === 'no' || wait == 'yes')))
{
changeColor = true;
}

if(changeColor)
{
document.getElementById("holdtr").style.backgroundColor = '#FF0000';
document.getElementById("waittr").style.backgroundColor = '#FF0000';
return false;
}
return true;
}
</script>
</head>
<body>

<form name="datepick" id="datepick" onsubmit="return check();">
<table>
<tr id="holdtr">
<td>
<select name="onHold" id="onHold">
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
</td>
<tr>
<tr id="waittr">
<td>
<select name="waitResponse" id="waitResponse">
<option value="yes">Yes</option>
<option value="no">No</option>
<option value="na">N/A</option>
</select>
</td>
</tr>
<tr>
<td>
<input type="submit" value="submit" />
</td>
</tr>
</body>
</html>
 
Well, that's the reactive approach, but I would like the proactive one: populate the second drop down according to the first answer.

That way, there's no need for validation.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top