Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2003
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    incorrect works, huh?

    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

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    New to the CF scene
    Join Date
    Jun 2003
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,042
    Thanks
    0
    Thanked 250 Times in 246 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •