Javascript help: Two scripts on one page

rickardo40

Member
Feb 13, 2001
63
0
0
Hi Guys. I did a search and didn't find anything helpful so I figured I'd ask. Here's my problem. I have a form that I am using a JS to write the information to a cookie for future retrieval. That works fine. Now I want to add a form validation script that will display a message if the user leaves a field blank. I tried adding the second script exactly like the instructions said and it wouldn't work. So I went ahead and tried the script in another form without any JS and it worked. So basically I realized that there must be some trick to getting two seperate scripts to work together on one page. I mean, I see it all over the place so it must be possible???

Anyway, here is the head script for the form information storage via cookies.
<SCRIPT language="JavaScript">
<!--
var never = new Date()
never.setTime(never.getTime() + 2000*24*60*60*1000);

// name is a string of the name of your cookie
// value is the value corresponding to name
function SetCookie(name, value) {
var expString = "; expires=" + never.toGMTString();
document.cookie = name + "=" + escape(value) + expString;
}

// returns value of cookie or null if cookie does not exist
function GetCookie(name) {
var result = null;
var myCookie = " " + document.cookie + ";";
var searchName = " " + name + "=";
var startOfCookie = myCookie.indexOf(searchName);
var endOfCookie;
if (startOfCookie != -1) {
startOfCookie += searchName.length; // skip past name of cookie
endOfCookie = myCookie.indexOf(";", startOfCookie);
result = unescape(myCookie.substring(startOfCookie, endOfCookie));
}
return result;
}

use_cookies = "unsure";

function saveValue(element) {
if (document.images && use_cookies == "unsure")
use_cookies = (confirm("Essentialapparel.com would like to save your Billing & Shipping Information. Doing so will make eliminate the need to re-enter the information each time you make a purchase. "
+"Would you like to save it?" +" Click OK to save and Cancel to continue without saving.") ? "yes":"no");
if (document.images && use_cookies == "yes") {
if ((element.type == "text")
|| (element.type == "password")
|| (element.type == "textarea")
|| (element.type == "radio")) {
val = element.value;
} else if (element.type.indexOf("select") != -1) {
val = "";
for(k=0;k<element.length;k++)
if (element.options[k].selected)
val += k+" ";
} else if (element.type == "checkbox") {
val = element.checked;
}
SetCookie("memory_"+element.form.name+"_"+element.name,val);
}
}

function storedValues() {
if (document.images) { // only do it in JavaScript 1.1 browsers
for (i=0;i<document.forms.length;i++) {
for (j=0;j<document.forms.elements.length; j++) {
cookie_name = "memory_"+document.forms.name+"_"
+document.forms.elements[j].name;
val = GetCookie(cookie_name);
if (val) {
if ((document.forms.elements[j].type == "text")
|| (document.forms.elements[j].type == "password")
|| (document.forms.elements[j].type == "textarea")) {
document.forms.elements[j].value = val;
} else if (document.forms.elements[j].type.indexOf("select") != -1) {
document.forms.elements[j].selectedIndex = -1;
while (((pos = val.indexOf(" ")) != -1) && (val.length > 1)) {
sel = parseInt(val.substring(0,pos));
val = val.substring(pos+1,val.length);
if (sel < document.forms.elements[j].length)
document.forms.elements[j].options[sel].selected = true;
}
} else if (document.forms.elements[j].type == "checkbox") {
document.forms.elements[j].checked = val;
} else if (document.forms.elements[j].type == "radio") {
if (document.forms.elements[j].value == val)
document.forms.elements[j].checked = true;
}
}
}
}
}
}

window.onload = storedValues;

// -->
</SCRIPT>

What would I do to get a second script like this one below to work with the one above.

<SCRIPT LANGUAGE="JavaScript">
<!--
function checkform ( form )
{
// ** START **
if (form.email.value == "") {
alert( "Please enter your email address." );
form.email.focus();
return false ;
}
// ** END **
return true ;
}
//-->
</SCRIPT>

Thanks a lot!