Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,359 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,211 people online right now. Registration is fast and FREE... Join Now!




Form Validtaion/Undesired results

 
Reply to this topicStart new topic

Form Validtaion/Undesired results, Need to validate one field or the other...

PonyDriver
post 30 Sep, 2008 - 10:37 AM
Post #1


New D.I.C Head

*
Joined: 27 Apr, 2007
Posts: 18


My Contributions


Hi Everyone,

My name is Tim and I have an assignment that I have completed but I can't seem to figure out why I can't validate one field or the other. The "Name" fields are required and that part of the validation works, but also required are the "Address" OR the "Email" fields. The form submits if only the "Address" field and the "Name" fields are completed but the form will not submit if only the "Email" field and the "Name" fields are completed. This is my problem, the form should submit if the "Name" fields AND EITHER of the "Address" or "Email" fields are completed. I want to say thank you in advance for any help offered, I have been struggling with this code for two days now and just can't seem to figure out what I am missing or where I am going wrong.

Here is the code:

CODE


<html>
<head>

<!--
    Timothy Shannon
    WEB406 - Week 4
    Individual Assignment
    Complete Change Request - #2
    on Service Request - SR-kf-011
-->
<!--
    Page title
-->
<title>Kudler Fine Foods Specials and Events Mailing List Registration Form</title>


<script type="text/javascript">

<!-- Form validation -->

function submitForm() {
if (document.forms[0].fname.value == ""
    || document.forms[0].lname.value == "") {
    window.alert("You must enter your first and last names.");
    return false;
}
else
if (document.forms[0].address.value == "") {
    window.alert("You must enter your mailing address or email address.");
    return false;
}
else
if (document.forms[0].email.value == "")
return true;
}
</script>
</head>

<form name="request" id="request" action="FormProcessor.html"
onsubmit="return submitForm();"
onreset="return confirmReset();"
enctype="application/x-www-form-urlelncoded">

<table border="0" cellspacing="0" cellpadding="5" width="620" >

<thead style="font-family: times-new roman">

<!--
    Header and logo
-->

<tr>
   <td valign="top" width="180" rowspan="2">
      <img src="Kudler.jpg" alt="kudler" width="375" height="200" >
   </td>
   <td style="font-size: 2.2em; color: black; font-weight: bold; font-style: italic"
       valign="top" align="center" >
         Kudler Fine Foods Specials and Events Mailing List Registration
   </td>
</tr>
</thead>

<!-- Customer information header -->
<tr>
   <td style="font-family: times-new roman; font-size: large; font-weight: bold"
       colspan="2" width="100%">
         Customer Information
   </td>
</tr>
<tr>
   <td style="font-family: times-new roman; color: red; font-size: small; font-style: italic"
       colspan="2" width="100%">
         * Required
         ** Either is required
   </td>
</tr>
<tr>
   <td colspan="2">
      <hr style="color: red; background-color: red; height: 3" >
   </td>
</tr>


<!-- Input fields -->
<tr>  
   <td valign="top" colspan="2">
   <table>
    <tr>
        <td width="50"><label for="fname">First</label></td>
        <td><input type="text" name="fname" id="fname" size="30" ><label style="color: red">*</label></td>
        <td width="50"><label for ="lname">Last</label></td>
        <td><input type="text" name="lname" id="lname" size="30" ><label style="color: red">*</label></td>
    </tr>
    <tr>
        <td width="100"><label for ="address">Address</label></td>
        <td><input type="text" name="address" id="address" size="60" ><label style="color: red">**</label></td>
    </tr>
    <tr>
    <td><label for="phone">Phone</label></td>
        <td><input type="text" name="phone" id="phone" size="12" >
    </td>
    </tr>
    <tr>
        <td><label for="email">Email Address</label></td>
        <td><input type="text" name="email" id="email" size="40" ><label style="color: red">**</label></td>
    </tr>
    
    </table>
   </td>
    <tr>
        <td valign="top">
        <input type="checkbox" name="Purpose" id="mail" value="Mailing list" >
        <label for="mail">Add me to your Mailing List</label><br >
        </td>
    </tr>
<tr>
   <td colspan="2">
      <hr style="color: red; background-color: red; height: 3" >
   </td>
</tr>
<!--
Customer comments
-->
<tr>
<td width="100" valign="top">
<label for="comments">Comments</label>
</td>
<td valign="top">
<textarea name="comments" id="comments" rows="6" cols="40">
</textarea>
</td>
</tr>

<!-- Register/cancel buttons -->
<tr>
   <td valign="top" colspan="2" align="center">
   <input type="Submit" value="Register" >
   <input type="reset" value="Cancel" >
   </td>
  
</tr>

<!-- Company name and locations -->

<tr style="background-color: red">
   <td colspan="2" align="center" valign="bottom">
      <address style="font-family: times-new roman; font-size: 0.6em; font-style: normal; font-size: small; color: black">
      Kudler Fine Foods ˇ Shopping the World for the Finest Foods ˇ La Jolla ˇ Del Mar ˇ Encinitas ˇ
      </address>
   </td>
</tr>
</table>


</form>

</html>




Thank you again in advance for any help offered, this site has been very helpful to me in the past, not just by searching on topics, but by getting answers to questions when I've been stumped as well.

"Pulling my hair out!" :-)

Tim
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 30 Sep, 2008 - 12:01 PM
Post #2


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,923



Thanked 118 times

Dream Kudos: 8475

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions


Try these changes to your JavaScript (Keep in mind it isn't tested but it should be pretty close to what you need)

jscript

<script type="text/javascript">

<!-- Form validation -->

function submitForm()
{
if (document.forms[0].fname.value == "" || document.forms[0].lname.value == "") {
window.alert("You must enter your first and last names.");
return false;
}
else
{
if (document.forms[0].address.value == "" && document.forms[0].email.value == "")
{
window.alert("You must enter either a mailing address or email address.");
return false;
}
else
{
return true;
}
}
}
</script>
User is offlineProfile CardPM

Go to the top of the page

PonyDriver
post 30 Sep, 2008 - 12:17 PM
Post #3


New D.I.C Head

*
Joined: 27 Apr, 2007
Posts: 18


My Contributions


QUOTE(PsychoCoder @ 30 Sep, 2008 - 01:01 PM) *

Try these changes to your JavaScript (Keep in mind it isn't tested but it should be pretty close to what you need)

jscript

<script type="text/javascript">

<!-- Form validation -->

function submitForm()
{
if (document.forms[0].fname.value == "" || document.forms[0].lname.value == "") {
window.alert("You must enter your first and last names.");
return false;
}
else
{
if (document.forms[0].address.value == "" && document.forms[0].email.value == "")
{
window.alert("You must enter either a mailing address or email address.");
return false;
}
else
{
return true;
}
}
}
</script>


User is offlineProfile CardPM

Go to the top of the page

PonyDriver
post 30 Sep, 2008 - 12:23 PM
Post #4


New D.I.C Head

*
Joined: 27 Apr, 2007
Posts: 18


My Contributions


PsychoCoder,

I sent you a thank you via the "This post was helpful link" I hope you got it, but because I wasn't sure how that would work, I wanted to say thank you here as well, the changes you suggested really did the trick. I got to say this place rocks! I was dreaming in code last night, well for the 4 hours sleep I got anyway, and my dreams didn't hold the answer, but Dream.In.Code did!


Tim
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 04:30AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month