Click to See Complete Forum and Search --> : Need Help Combining 2 Scripts With onSubmit Event Handlers


dansajd
11-10-2003, 10:57 AM
I have a form in which I must accomplish three things before submission:

1. Field Validation
2. Email Address Confirmation
3. User must supply a name -or- a phone number of the person that referred them (for the sake of readability, I'll refer to this as "either/or" in this post).

I found three scripts from The JavaScript Source that meet my needs. As such, individually, I can accomplish all three items. The problem is, I can't seem to find a way to do all three at once.

I've read over numerous threads on combining onSubmit event handlers and have tried many different variations, but I keep coming up empty.

I'm not having any problems with field validation. This is being handled through an onClick event handler in the form. I can use either the Email Address Confirmation or the either/or script without an issue in combination with the field validation. I really need both to work.

Here are the individual scripts:

Email Confirmation (modified password confirmation script)

function validatePwd() {
var invalid = " "; // Invalid character is a space
var minLength = 6; // Minimum length
var pw1 = document.join.From.value;
var pw2 = document.join.From2.value;
// check for a value in both fields.
if (pw1 == '' || pw2 == '') {
alert('Please enter your email address twice.');
return false;
}
// check for minimum length
if (document.join.From.value.length < minLength) {
alert('Your email address must be at least ' + minLength + ' characters long. Try again.');
return false;
}
// check for spaces
if (document.join.From.value.indexOf(invalid) > -1) {
alert("Sorry, spaces are not allowed in your email address.");
return false;
}
else {
if (pw1 != pw2) {
alert ("You did not enter the same email address twice. Please re-enter your email address.");
return false;
}
}
}


Eiter/Or

function checkFields() {
name = document.submitform.name.value;
email = document.submitform.email.value;

if ((name == "") && (email == "") ) {
alert("Please enter your name or your email address.");
return false;
}
else return true;
}


I've tried adding:
onSubmit="return validatePwd(); return checkFields()"
onSubmit="return validatePwd(); checkFields()"

I've also tried combining both scripts into one:

function validatePwd() {
var invalid = " "; // Invalid character is a space
var minLength = 6; // Minimum length
var pw1 = document.join.From.value;
var pw2 = document.join.From2.value;
var name = document.join.refername.value;
var email = document.join.referemail.value;
// check for a value in both fields.
if (pw1 == '' || pw2 == '') {
alert('Please enter your e-mail address twice.');
return false;
}
// check for minimum length
if (document.join.From.value.length < minLength) {
alert('Your e-mail address must be at least ' + minLength + ' characters long. Try again.');
return false;
}
// check for spaces
if (document.join.From.value.indexOf(invalid) > -1) {
alert("Sorry, spaces are not allowed in your e-mail address.");
return false;
}
else {
if (pw1 != pw2) {
alert ("You did not enter the same e-mail address twice. Please re-enter your e-mail address.");
return false;
}
}
else {
if (name == '' && email == '') {
alert("Please enter the name or e-mail address of the person who referred you.");
return false;
}
}
}


This last one gets me the closest to accomplishing what I need it to do. The problem is that when I click on the "OK" button on the alert box, the form gets submitted anyway.

I am ridiculously lost. Can someone please help me figure out what I need to do to get both these scripts to work together?

I apologize if I left anything out and for the length of this post. Please let me know if you require further information.

96turnerri
11-10-2003, 11:21 AM
onclick="validatePWD();checkFields()"

dansajd
11-10-2003, 01:25 PM
Originally posted by 96turnerri
onclick="validatePWD();checkFields()"

Thanks for the reply, but that doesn't work either. Actually, I lose the functionality of both scripts that way. Here's what my code ends up looking like:

<input type="submit" value="Submit" name="Submit" onClick="validate();return returnVal;validatePwd();checkFields()">

The first two sections are for the form field validation, called from an external JS.

If I submit the form, field validation works, but it doesn't run the other two functions at all.

dansajd
11-10-2003, 02:32 PM
I think I finally have it working. I wanted to post the solution just in case anyone else can make use of it.

I must have been screwing up the syntax somewhere in my first attempts to combine the two scripts, but that's what I ultimately did in order to get it to work.

Here's the final script:
function validatePwd() {
var invalid = " "; // Invalid character is a space
var minLength = 6; // Minimum length
var pw1 = document.join.From.value;
var pw2 = document.join.From2.value;
var refername = document.join.refername.value;
var refernum = document.join.refernum.value;
// check for a value in both fields.
if (pw1 == '' || pw2 == '') {
alert('Please enter your email address twice.');
return false;
}
// check for minimum length
if (document.join.From.value.length < minLength) {
alert('Your email address must be at least ' + minLength + ' characters long. Try again.');
return false;
}
// check for spaces
if (document.join.From.value.indexOf(invalid) > -1) {
alert("Sorry, spaces are not allowed in your email address.");
return false;
}
else {
if (pw1 != pw2) {
alert ("You did not enter the same email address twice. Please re-enter your email address.");
return false;
}
}
if (refername == '' && refernum == '') {
alert("Please enter the name or phone number of the Member who referred you.");
return false;
}
else return true;
}

I then call this from the onSubmit event handler in the form tag as follows:

<form name="join" action="cgi-bin/mem2memrcpt.cgi" method="post" onSubmit="return validatePwd()">

96turnerri
11-10-2003, 06:03 PM
why are you doing the return bit just ignore that bit it shouldnt be needed if coding correct its either onclick="dfdsf();sdas()" or onclick="dsfsd(); dfdsf()"