...

View Full Version : incorrect works, huh?



sierra
06-01-2003, 09:40 PM
Hello,

I have three scripts on my page and I want to execute three functions on the submission of a form, a friend told me to call them in the following manner.

onSubmit="return formCheck(formobj); domainCheck(entry); submitonce(theform);"

Now, I understand that this is the correct way to call them, however, when I call them in that manner nothing executes. Which leads me to believe there must be some conflict in the actual code of the functions.

When I call them in the below fashion, they all execute properly and yield the results I want to achieve.

onSubmit="return formCheck(formobj) && domainCheck(entry) && submitonce(theform);"

My friend has indicated that this is not the correct way to accomplish this and I should correct the code in the functions so that I can execute in the correct manner, the first example.

Now, I'm no programmer and can't fix the code, but my question is what is the difference between the above two examples? And why would it work one way and not the other? Hugs and kisses to anyone willing to explain this to me, thank you much!

Sierra

Garadon
06-01-2003, 09:47 PM
a llose guess is that the first actually do all 3 functions but only return the result of the first.

where as the others return result of all 3 cause u used && between them which in theory should be the same as writing :)

onSubmit="return formCheck(formobj);return domainCheck(entry);return submitonce(theform);"


but without ur code it is merely a guess :)

sierra
06-02-2003, 12:11 AM
Thanks Garadon,

Are you saying it's OK to do it the way I am or not OK?

Below is the full code if that helps you.

Thanks again hun, Sierra.



<script language="javascript" type="text/javascript">
<!--

function submitonce(theform){
//if IE 4+ or NS 6+
if (document.all||document.getElementById){
//screen thru every element in the form, and hunt down "submit" and "reset"
for (i=0;i<theform.length;i++){
var tempobj=theform.elements[i]
if(tempobj.type.toLowerCase()=="submit"||tempobj.type.toLowerCase()=="reset")
//disable em
tempobj.disabled=true
}
}
}


function formCheck(formobj){
//1) Enter name of mandatory fields
var fieldRequired = Array("firstname", "lastname", "phone", "email", "city", "state", "referer");
//2) Enter field description to appear in the dialog box
var fieldDescription = Array("First Name", "Last Name", "Telephone Number", "Your E-mail Address", "City", "State", "How did you hear about us?");
//3) Enter dialog message
var alertMsg = "Please complete the following fields:\n";

var l_Msg = alertMsg.length;

for (var i = 0; i < fieldRequired.length; i++){
var obj = formobj.elements[fieldRequired[i]];
if (obj){
switch(obj.type){
case "select-one":
if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
case "select-multiple":
if (obj.selectedIndex == -1){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
case "text":
case "textarea":
if (obj.value == "" || obj.value == null){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
default:
if (obj.value == "" || obj.value == null){
alertMsg += " - " + fieldDescription[i] + "\n";
}
}
}
}

if (alertMsg.length == l_Msg){
return true;
}else{
alert(alertMsg);
return false;
}
}



function domainCheck(entry){
var no_good = new Array("hotmail","rocketmail","yahoo","bigfoot","mailexcite","usa.net");
entry = entry.toLowerCase();
var ok = true;
for (i=0; i<=no_good.length-1;i++){
if(entry.indexOf(no_good[i])>=0) ok = false;
}
if (ok) {
return true;
}else{
document.theform.email.value = "";
document.theform.email.focus();
alert("Sorry, we cannot accept requests from free email services");
return false;
}
}

//-->
</script>


on the form tag I use

name="theform" onSubmit="return formCheck(formobj) && domainCheck(entry) && submitonce(theform);"

Thanks!

Sierra

glenngv
06-02-2003, 02:14 AM
if you call them like this:

onSubmit="return formCheck(formobj); domainCheck(entry); submitonce(theform);"

only formCheck() function will be called because of the return.

so this is the correct one:

onSubmit="return formCheck(this) && domainCheck(entry) && submitonce(this);"

you can just use the this keyword to reference the form.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum