Form Validation and Cookies
Hi there,
I am fairly new to JavaScript and having some difficulty.
The following code represents a prototype for a mailing list sign-up.
The user must either submit their name and email OR name and mailing address. I have the validations working on Safari and Chrome, but not IE and Firefox. I have changed the pop-up settings, but that was unsuccessful. Any suggestions?
ALSO -
I need to set a cookie when the form is submitted successfully. If the user tries to resubmit, I would like a new HTML page advising them that they have already signed up. I have absolutely no idea how to accomplish this.
Here is my code. Any help would be greatly appreciated.
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Mailing List: Sign Up</title>
<link href="Main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript"/>
function validateForm() {
//check email variables
var email = document.getElementById('email').value;
var elength = email.length;
var atpos = email.indexOf('@');
var atdot = email.lastIndexOf('.');
var tld = email.substring(atdot);
//other variables
var feedback = ""
var feedbackFN = ""
var feedbackLN = ""
var feedbackSA = ""
var feedbackC = ""
var feedbackState = ""
var feedbackZ = ""
var firstName = document.getElementById('firstname')
var lastName = document.getElementById('lastname')
var streetAdd = document.getElementById('streetaddress')
var city = document.getElementById('city')
var stateName = document.getElementById('state')
var zip = document.getElementById('zipcode')
// if all
if ((firstName.value !="") && (lastName.value !="") && (streetAdd.value !="") && (city.value !="") && (state.value >= 1) && (zip.value !="") && (atpos > 1 || atdot > 1 || atdot > atpos)) {
window.open('MailingList_Confirmation.html')
return true;
}
//if name and address
else if ((firstName.value !="") && (lastName.value !="") && (streetAdd.value !="") && (city.value !="") && (state.value >= 1) && (zip.value !="")) {
window.open('MailingList_Confirmation.html')
return true;
}
//if name and email
else if ((firstName.value !="") && (lastName.value !="") && (atpos > 1 || atdot > 1 || atdot > atpos)) {
window.open('MailingList_Confirmation.html')
return true;
}
else if (firstName.value=="") {
feedbackFN += "<strong style='color:red'>This is a required field.</strong>";
document.getElementById('feedbackFN').innerHTML = feedbackFN;
document.getElementById('firstname').style.borderColor="red";
}
if (lastName.value=="") {
feedbackLN += "<strong style='color:red'>This is a required field.</strong>";
document.getElementById('feedbackLN').innerHTML = feedbackLN;
document.getElementById('lastname').style.borderColor="red";
}
if (streetAdd.value=="") {
feedbackSA += "<strong style='color:red'>This is a required field.</strong>";
document.getElementById('feedbackSA').innerHTML = feedbackSA;
document.getElementById('streetaddress').style.borderColor="red";
}
if (city.value=="") {
feedbackC += "<strong style='color:red'>This is a required field.</strong>";
document.getElementById('feedbackC').innerHTML = feedbackC;
document.getElementById('city').style.borderColor="red";
}
if (state.value < 1) {
feedbackState += "<strong style='color:red'>This is a required field.</strong>";
document.getElementById('feedbackState').innerHTML = feedbackState;
document.getElementById('state').style.borderColor="red";
}
if (zip.value=="") {
feedbackZ += "<strong style='color:red'>This is a required field.</strong>";
document.getElementById('feedbackZ').innerHTML = feedbackZ;
document.getElementById('zipcode').style.borderColor="red";
}
if (atpos < 1 || atdot < 1 || atpos > atdot) {
feedback += "<strong style='color:red'>Email is not valid</strong>";
document.getElementById('feedback').innerHTML = feedback;
document.getElementById('email').style.borderColor="red";
}
return false;
}
</script>
<style>
form { width: 500px; }
label { float: left; width: 150px; font-family:"Times New Roman", Times, serif; }
input[type=text] { float: left; width: 200px; }
</style>
</head>
<body>
<form name="input" onsubmit="return validateForm()" action="form_info_input.asp" method="post"/>
<!--First Name-->
<label for="First name">First name:</label><input type="text" name="firstname" id="firstname"/><span style="color:#F00">*</span><p id="feedbackFN"></p>
<!--Last Name-->
<label for="Last name">Last name: </label><input type="text" name="lastname" id="lastname"/><span style="color:#F00">*</span><p id="feedbackLN"></p>
<!--Street Address-->
<label for="Street address">Street Address Line 1: </label><input type="text" name="streetaddress" id="streetaddress"/><span style="color:#F00">*</span><p id="feedbackSA"></p>
<!--Street Address 2-->
<label for="Street address two">Street Address Line 2:</label><input type="text" name="streetaddresstwo" id="streetaddress2"/><br/><br/>
<!--City-->
<label for="City">City: </label><input type="text" name="city" id="city"/><span style="color:#F00">*</span><p id="feedbackC"></p>
<!--State-->
<label for="State">State: </label><select name="state" id="state" tabindex="150"/><br/><br/>
<option value="0">Select State</option>
<option value="1">Alabama</option>
<option value="2">Alaska</option>
<option value="3">Arizona</option>
<option value="4">Arkansas</option>
<option value="5">California</option>
<option value="6">Colorado</option>
<option value="7">Connecticut</option>
<option value="8">Delaware</option>
<option value="9">Florida</option>
<option value="10">Georgia</option>
<option value="11">Hawaii</option>
<option value="12">Idaho</option>
<option value="13">Illinois</option>
<option value="14">Indiana</option>
<option value="15">Iowa</option>
<option value="16">Kansas</option>
<option value="17">Kentucky</option>
<option value="18">Louisiana</option>
<option value="19">Maine</option>
<option value="20">Maryland</option>
<option value="21">Massachusetts</option>
<option value="22">Michigan</option>
<option value="23">Minnesota</option>
<option value="24">Mississippi</option>
<option value="25">Missouri</option>
<option value="26">Montana</option>
<option value="27">Nebraska</option>
<option value="28">Nevada</option>
<option value="29">New Hampshire</option>
<option value="30">New Jersey</option>
<option value="31">New Mexico</option>
<option value="32">New York</option>
<option value="33">North Carolina</option>
<option value="34">North Dakota</option>
<option value="35">Ohio</option>
<option value="36">Oklahoma</option>
<option value="37">Oregon</option>
<option value="38">Pennsylvania</option>
<option value="39">Rhode Island</option>
<option value="40">South Carolina</option>
<option value="41">South Dakota</option>
<option value="42">Tennessee</option>
<option value="43">Texas</option>
<option value="44">Utah</option>
<option value="45">Vermont</option>
<option value="46">Virginia</option>
<option value="47">Washington</option>
<option value="48">West Virginia</option>
<option value="49">Wisconsin</option>
<option value="50">Wyoming</option>
</select><span style="color:#F00">*</span><p id="feedbackState"></p>
<!--Zip Code-->
<label for="Zip code">Zip Code: </label><input type="text" name="zipcode" id="zipcode"/><span style="color:#F00">*</span><p id="feedbackZ"></p>
<!--Email-->
<label for="Email address">Email Address: </label><input type="text" name="emailaddress" id="email"/><span style="color:#F00">*</span><p id="feedback"></p>
<!--Telephone-->
<label for="Telephone number">Telephone Number: </label><input type="text" name="telephonenumber" id="phone"/><br/><br/>
<input type="submit" value="Subscribe Now" id="submit"/>
<input type="reset" value="Clear"/>
</form>
</body>
</html>
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Tags for this Thread
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks