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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if/then between 2 form fields

Status
Not open for further replies.
Apr 6, 2001
41
0
0
US
I apologize for my newbie-ness but I need help writing an easy script. Hope this isn't too much of a faux pas.

I have two "email" textarea fields in a form. In order, the first field is named "EmailAddress" (required) and the second is "ContactEmail" (we're using both addresses for different things. both addresses are actually Response.write'ed in the sent email message).

Here's what I need: If a user enters email addresses in BOTH fields then the address in "ContactEmail" is the From address in the email that is sent to us. If the user only fills in "EmailAddress" and NOT "ContactEmail" at all, then "EmailAddress" is the From address of the email that is sent to us.

I really appreciate the help!
 
<!-- This code should help if I understand what you want right.
I didn't write the isEmpty and IsWhitespace functions I got them off a site, can remember which one though.
Thanks whoever wrote them
-->

<html>
<head>
<script language=&quot;JavaScript&quot;>
<!--
// whitespace characters
var whitespace = &quot; \t\n\r&quot;;

function isEmpty(s)
{ return ((s == null) || (s.length == 0))
}

function isWhitespace (s)
{ var i;
// Is s empty?
if (isEmpty(s)) return true;
// Search through string's characters one by one
// until we find a non-whitespace character.
// When we do, return false; if we don't, return true.
for (i = 0; i < s.length; i++)
{
// Check that current character isn't whitespace.
var c = s.charAt(i);
if (whitespace.indexOf(c) == -1) return false;
}
// All characters are whitespace.
return true;
}


function zzz(){
if (isWhitespace(document.form1.address2.value)){
// put the stuff you want to do if only the one address is filled in over here
alert(&quot;1&quot;);
} else {
// put the code to send to both addresses here
alert(&quot;2&quot;);
}
}

-->
</script>
</head>
<body>
<form name=&quot;form1&quot;>
Email 1<input type=&quot;text&quot; name=&quot;address1&quot;><br>
Email 2<input type=&quot;text&quot; name=&quot;address2&quot;><br>
<input type=&quot;button&quot; onclick=&quot;zzz();&quot; value=&quot;Submit&quot;>
</form>
</body>
</html>
 
A two parter...

PART 1:

Damian13 - Thank you for finding it! And thank you to whomever wrote it! It's exactly what I was looking for!

But now in the function zzz(), how do I get the address field to just go ahead and submit with the rest of the form instead of the test alerts 1 or 2? ...See? I really am a newbie. (-:


PART 2:

Also, I have an onFocus function on the ContactEmail field...

function clearfield(){
if (document.Form.ContactEmail.value == &quot;Contact Email Address Here&quot;)
document.Form.ContactEmail.value = &quot;&quot;;

I would like to call the same function by an onSubmit but I also have form validation being called by the onSubmit. Is it possible to connect two separate function calls from one onSubmit? How?

The answers to my questions are probably simple but I don't know, that's why I ask. Thank you ALL for your help!
 
PART 2
<!--
This way of doing it is cool, because you make your functions return TRUE / FALSE so if the first one fails it doesnt waist time doing the next one and the form is only submitted when both return TRUE

but if you just want it plain
onSubmit=&quot;test1();test2();&quot;
that will run both of the functions no matter what

-->
<HTML>
<HEAD>
<script language=&quot;JavaScript&quot;>
<!--
function test1(){
alert('a');
return true;
}
function test2(){
alert('b');
return true;
}
-->
</script>
</HEAD>
<BODY>
<CENTER><H2>Test Screen</H2></CENTER>
<CENTER>
<FORM Name=&quot;Form1&quot; ACTION=&quot;2.html&quot; onSubmit=&quot;return test1() && test2();&quot;>
Name<INPUT TYPE=&quot;text&quot; NAME=&quot;name&quot; size = &quot;20&quot;>
<br><input type=&quot;submit&quot; >
</FORM>
</CENTER>
</BODY>
</HTML>

PART 1 ... I am a newbie 2
The way I would do it is just to submit the form and when it is submitted you can get the values out of your email fields and do whatever checks you want ... Unfortunately, I use PHP to do this and I dont think it is what you want
but this is how you do it incase
:) :p b-) :-| I like these faces ... :)
in the form that is going to be submitted you have ...

<form name=&quot;form1&quot; method=&quot;post&quot; action=&quot;2.html&quot;>
<input type=&quot;text&quot; name=&quot;username&quot;>
<input type=&quot;submit&quot;>
</form>

then in 2.html you put this ( <? ?> php stuff )
<?
$username = $HTTP_POST_VARS['username'];
?>

Then you got username in the new document.

you HAVE to be able to do this with JavaScript, but I havent seen it yet, sorry.
If you use PHP I will explain more if you want.BUT then again you wouldnt be at this forum if you did :)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top