6 Replies - 533 Views - Last Post: 20 June 2012 - 09:45 AM

#1 OldLearner  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 10-May 12

Having a problem with my code

Posted 19 June 2012 - 09:48 PM

I have written my code out, but I am suppose to have an alert if the customer doesn't enter their address or their email address. I thought I had it, but I tried to check it by just keying in the name and hit submit button but it didn't alert stating you must enter address or email , it just went to the acknowledgement page.
Here is the code I have done.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta name="generator" content="HTML Tidy for Linux (vers 6 November 2007), see www.w3.org" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Kudler Fine Foods Customer Mailing List</title>

<style type="text/css">
/*<![CDATA[*/
body
{
background-color: #ECE4DF;
}
/*]]>*/
</style>

<style type="text/css">
/*<![CDATA[*/
 div.c1 {text-align: center}
/*]]>*/
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
/*<![CDATA[*/
 tr.c6 {background-color: blue}
 address.c5 {font-family: sans-serif; font-size: 0.6em; font-style: normal; color: white}
 hr.c4 {color: blue; background-color: blue; height: 1}
 td.c3 {font-family: sans-serif; font-size: large; font-weight: bold}
 thead.c2 {font-family: sans-serif}
 td.c1 {font-size: 1.2em; color: black; font-weight: bold; font-style: italic}
/*]]>*/
</style>
</head>
<body>
<div class="c1">
<h3>Cover Page</h3>
</div>
<div class="c1">
<h2>************</h2>
</div>
<div class="c1"> ********</div>
<div class="c1"> SR-kf-011</div>
<div class="c1">Requester: Yvonne Reynolds</div>
<div class="c1">Kudler Fine Foods Customer Mailing List</div>
<script type="text/javascript">
//<![CDATA[
<!-- Define cookie -->
set_cookie("LoggedIn",true,10);
function set_cookie(fname, value, days) 
{
if (days) 
{
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
var expires = "; expires=" + date.toGMTString();
} else var expires = ""; 
var host = "your_domain.com";
var domain = "; domain=" + host;
document.cookie = fname + "=" + value + expires + domain + "; path=/";
get_cookie("LoggedIn");
function get_cookie(fname)
{
var EQ = fname + "=";
var ca = document.cookie.split(';');
for (var i=0;i < ca.length;i++)
{
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(EQ) == 0) return c.substring(EQ.length,c.length);
}
return null;
}
<!-- Form Submission check -->
function submitForm() {
 onsubmit="return 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].address1.value == "")
{ 
window.alert("You must enter a mailing address or email address."); 
return false;  
}
else 
{
 return true;
 }
 }
 }
 
|| document.forms[0].city.value == ""
|| document.forms[0].city.value == ""
|| document.forms[0].state.value == ""
|| document.forms[0].zip.value == ""
|| document.forms[0].phone.value == ""
window.alert("You must enter your information.");
return false;
}
return true;
//]]>
</script>
<form name="request" id="request" action="AcknowledgementPage.html" onsubmit="return submitForm();
" onsubmit="return validate_form();" method="get" enctype="application/x-www-form-urlelncoded">
<table border="0" cellspacing="0" cellpadding="5" width="620">
<thead class="c2"><!-- Company Logo and Heading -->
<tr>
<td valign="top" width="180" rowspan="2"><img src="Kudler-food-blk.gif" alt="kudler" width="275" height="175" /></td>
<td class="c1" valign="top" align="center">Kudler Fine Foods Customer Information Form</td>
</tr>
</thead>
<!-- Customer Information Heading -->
<tr>
<td class="c3" colspan="2" width="100%">Customer Information</td>
</tr>
<tr>
<td colspan="2">
<hr class="c4" /></td>
</tr>
<!-- Contact Information to be input -->
<tr>
<td valign="top" colspan="2">
<table>
<tr>
<td width="100"><label for="fname">First Name</label></td>
<td><input type="text" name="fname" id="fname" size="30" >
<td><label for ="lname">Last Name</label></td>
<td><input type="text" name="lname" id="lname" size="30" >   
</td>
</tr>

<tr>
<td><label for ="address1">Address</label></td>
<td><input type="text" name="address" id="address" size="60" ></td>  

</tr>
<tr>

<td><label for="city">City</label></td>

<td><input type="text" name="city" id="city" size="40" /><label 

for="state">State</label> 
<input type="text" name="state" id="state" size="3">

<td><label for="zip">ZIP</label></td>

<td><input type="text" name="zip" id="zip" size="10" maxlength="10">
</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"></td>
</tr>
</table>
</td>
<tr>
<td valign="top">
<input type="checkbox" name="use" id="mail" value="mail" >
<label for="mail">Add me to your Mailing List</label><br />
</td>
</tr>

<tr>
<td colspan="2">
<hr style="color: #099; background-color: #099; height: 1" >  
</td>

</tr>

<!-- Customer Comments Section -->

<tr>
<td width="100" valign="top">
<label for="comments">Comments</label>
</td>
<td valign="top">

<textarea name="comments" id="comments" rows="6" cols="50">
</textarea>
</td>
</tr>


<!-- Buttons for submission or cancelation -->

<tr>
<td valign="top" colspan="2" align="center">
<input type="submit" value="Submit">
<input type="reset" value="Cancel" />
</td>

<!-- Company Name and locations -->
<tr class="c6">
<td colspan="2" align="center" valign="bottom">
<address class="c5">Kudler Fine Foods ?Shopping the World for the Finest Foods ?La Jolla ?Del Mar ?Encinitas ?/address>
</td>
</tr>
</table>
</form>
</body>
</html>




Am I just not listing it in the right place?
can someone steer me in the right direction?

This post has been edited by no2pencil: 20 June 2012 - 01:51 AM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Having a problem with my code

#2 kentlucky  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 48
  • Joined: 20-June 12

Re: Having a problem with my code

Posted 20 June 2012 - 12:46 AM

Hello,
I would like to suggest that you would use jquery to make your easier. I have attached an example about your problem using jquery.

You can also go to this page to know more about jquery
http://jquery.com/

and jquery validation
http://docs.jquery.c...gins/Validation

Thank you. I hope it helps.
Was This Post Helpful? 0
  • +
  • -

#3 OldLearner  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 10-May 12

Re: Having a problem with my code

Posted 20 June 2012 - 12:53 AM

I have to use Javascript
Was This Post Helpful? 0
  • +
  • -

#4 kentlucky  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 48
  • Joined: 20-June 12

Re: Having a problem with my code

Posted 20 June 2012 - 01:49 AM

View PostOldLearner, on 20 June 2012 - 03:53 PM, said:

I have to use Javascript

Oh, sorry for that.
The function below checks if the content has the general syntax of an email.

This means that the input data must contain an @ sign and at least one dot (.). Also, the @ must not be the first character of the email address, and the last dot must be present after the @ sign, and minimum 2 characters before the end:

function validateForm()
{
var x=document.forms["myForm"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
  {
  alert("Not a valid e-mail address");
  return false;
  }
}


Example:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function validateForm()
{
var x=document.forms["myForm"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
  {
  alert("Not a valid e-mail address");
  return false;
  }
}
</script>
</head>

<body>
<form name="myForm" action="demo_form.asp" onsubmit="return validateForm();" method="post">
Email: <input type="text" name="email">
<input type="submit" value="Submit">
</form>
</body>

</html>

Was This Post Helpful? 1
  • +
  • -

#5 kentlucky  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 48
  • Joined: 20-June 12

Re: Having a problem with my code

Posted 20 June 2012 - 01:58 AM

I hope it helps.
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2894
  • View blog
  • Posts: 7,541
  • Joined: 08-June 10

Re: Having a problem with my code

Posted 20 June 2012 - 06:45 AM

View PostOldLearner, on 20 June 2012 - 09:53 AM, said:

I have to use Javascript

jQuery IS (written in) Javascript.

think of it as a predefined non-native Javascript object.
Was This Post Helpful? 0
  • +
  • -

#7 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 72
  • View blog
  • Posts: 291
  • Joined: 13-May 09

Re: Having a problem with my code

Posted 20 June 2012 - 09:45 AM

First I would like to point out that your form has 2 onsubmit attributes

onsubmit="return submitForm();" onsubmit="return validate_form();"


Second, have you checked your error console to see if there is something broken in your JS that could be making the code fail and therefor not returning a false and submitting the form?

When looking at the javascript, i see an error around like 97
window.alert("You must enter a mailing address or email address."); 
return false;  
}
else 
{
 return true;
 }
 }
 }
 
|| document.forms[0].city.value == ""
|| document.forms[0].city.value == ""
|| document.forms[0].state.value == ""
|| document.forms[0].zip.value == ""
|| document.forms[0].phone.value == ""
window.alert("You must enter your information.");
return false;

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1