471,338 Members | 1,295 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

Use one field to validate values in one of two fields

I am using a script to validate a form using the presence of a value in
one field and determine if there is a value in one of two fields. It
is an either/or situation. If the date rcvd field is not "", then
either the ref names has to be filled in OR the res date has to be
filled in. I have tried many combinations of this. Any suggestions
will be greatly appreciated.

<script language="JavaScript">
function ValidateForm(){

var Rcv1Field = document.form1.DateRcvd
var Refer1Field = document.form1.RefNames
var FRDfield = document.form1.ResDate

if (Rcv1Field.value != "" && (Refer1Field.value == "" || FRDfield.value
!= "")){

if (Rcv1Field.value != "" && Refer1Field.value == "" ){
alert("Please Enter a Referral")
Refer1Field.focus()
return false
}
if (Rcv1Field.value != "" && FRDfield.value != ""){
alert("Please Enter a Final Resolution Date")
FRDfield.focus()
return false
}
}
return true
}
</script>

Jul 23 '05 #1
2 1778
Meredith wrote:
I am using a script to validate a form using the presence of a value in
one field and determine if there is a value in one of two fields. It
is an either/or situation. If the date rcvd field is not "", then
either the ref names has to be filled in OR the res date has to be
filled in. I have tried many combinations of this. Any suggestions
will be greatly appreciated.

<snip>

Does this do what you want? It requires the Date Received field to be
filled in.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled</title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
<script type="text/javascript">
function ValidateForm(){
var Rcv1Field = document.form1.DateRcvd;
var Refer1Field = document.form1.RefNames;
var FRDfield = document.form1.ResDate;
if (Rcv1Field.value == ""){
alert('Please Enter a Date Received');
return false;
}
if (Refer1Field.value == "" && FRDfield.value == ""){
alert("Please Enter a Referral Name OR a Resolution Date");
Refer1Field.focus();
return false;
}else if (Refer1Field.value != "" && FRDfield.value != ""){
alert("Please Enter ONLY ONE of the following:\n - a Final
Resolution Date\n - a Referral Name");
FRDfield.focus();
return false;
}
return true;
}
</script>
</head>

<body>
<form name="form1" action="newpage.htm" method="post">
Date Received&nbsp;<input type="text" name="DateRcvd"><br>
Referral Names&nbsp;<input type="text" name="RefNames"><br>
Resolution Date&nbsp;<input type="text" name="ResDate"><br><br>
<input type="button" value="Validate" onclick="ValidateForm()">
</form>
</body>
</html>
Jul 23 '05 #2
Thank you for your assistance....I was able to use the logic of the
suggestion to get the desired result. This is the snippet that I used:

if (Rcv1Field.value != ""){
if (Refer1Field.value == "" && FRDfield.value == ""){
alert("Please Enter a Referral Name OR a Resolution Date");
Refer1Field.focus();
return false;
}
}

The Date Rcvd field is not required, however, if it does have a date in
it, either the referral name or resolution date needs to have a value.
Thanks again.

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Mike | last post: by
2 posts views Thread by Doslil | last post: by
2 posts views Thread by Mufasa | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.