www.webdeveloper.com
Results 1 to 2 of 2

Thread: JAVASCRIPT: validation/form mail problem... HELP!!!!

  1. #1
    Join Date
    Nov 2009
    Posts
    1

    JAVASCRIPT: validation/form mail problem... HELP!!!!

    I am having an issue. First here is the scripts:

    Script 1 - Form Validation:

    Code:
    function formCheck(formobj){
    	// Enter name of mandatory fields
    	var fieldRequired = Array("ATTUID", "cname", "CBN", "WTN", "cbt", "cbdm", "cbdd", "case", "notes", "issue", "modem", "power", "ethernet", "dsl", "internet", "os", "filters", "router", "firewall", "ts1", "ts2");
    	// Enter field description to appear in the dialog box
    	var fieldDescription = Array("ATTUID", "Contact Name", "CBN", "WTN", "Call Back Time", "Call Back Date - Month", "Call Back Date - Day", "Case Number", "Reason For Call Back", "Issue Type", "Modem Model", "Power", "Ethernet", "DSL/Broadband", "Internet", "OS", "Filters", "Router", "Firewall", "Troubleshooting Step 1", "Troubleshooting Step 2");
    	// 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.type == undefined){
    				var blnchecked = false;
    				for (var j = 0; j < obj.length; j++){
    					if (obj[j].checked){
    						blnchecked = true;
    					}
    				}
    				if (!blnchecked){
    					alertMsg += " - " + fieldDescription[i] + "\n";
    				}
    			}
    		}
    	}
    
    	if (alertMsg.length == l_Msg){
    		return true;
    	}
    	else{
    		alert(alertMsg);
    		return false;
    	}
    }
    Script 2 - Web Form to Client Email Generator

    Code:
    function emailForm()
    { 
    
    
      var cent = document.MemoForm.cent.value;
      var attuid = document.MemoForm.ATTUID.value;
      var approved = document.MemoForm.approved.value;
      var issue = document.MemoForm.issue.value;
      var cname = document.MemoForm.cname.value;
      var modem = document.MemoForm.modem.value;
      var power = document.MemoForm.power.value;
      var ethernet = document.MemoForm.ethernet.value;
      var cbt = document.MemoForm.cbt.value;
      var cbdm = document.MemoForm.cbdm.value;
      var cbdd = document.MemoForm.cbdd.value;
      var CBN = document.MemoForm.CBN.value;
      var WTN = document.MemoForm.WTN.value;
      var dsl = document.MemoForm.dsl.value;
      var cas = document.MemoForm.cas.value;
      var internet = document.MemoForm.internet.value;
      var ethernet = document.MemoForm.ethernet.value;
      var os = document.MemoForm.os.value;
      var filters = document.MemoForm.filters.value;
      var notes = document.MemoForm.notes.value;
      var router = document.MemoForm.router.value;
      var firewall = document.MemoForm.firewall.value;
      var ts1 = document.MemoForm.ts1.value;
      var ts2 = document.MemoForm.ts2.value;
      var ts3 = document.MemoForm.ts3.value;
      var ts4 = document.MemoForm.ts4.value;
      var ts5 = document.MemoForm.ts5.value;
    
    var email = cent ;
    var subject = "Callback needed on " + cbdm + "/" + cbdd + " at " + cbt + "." ; 
    var body = "CALLBACK DETAILS" + "%0D%0D ATTUID: " + attuid + "%0D Approved By: " + approved + "%0D%0D Caller: " + cname + "%0D WTN: " + WTN + "%0D CBN: " + CBN + "%0D Case: " + cas + "%0D%0D Notes: " + "%0D " + notes + "%0D%0D TROUBLESHOOTING DETAILS" + "%0D%0D Issue: " + issue + "%0D Modem: " + modem + "%0D Pwr: " + power + "%0D Enet: " + ethernet + "%0D DSL: " + dsl + "%0D Inet: " + internet + "%0D%0D OS: " + os + "%0D Filters: " + filters + "%0D Firewall: " + firewall + "%0D Steps Taken: " + "%0D%0D -" + ts1 + "%0D -" + ts2 + "%0D -" + ts3 + "%0D -" + ts4 + "%0D -" + ts5;
    
    
    var mailto_link = 'mailto:'+email+'?subject='+subject+'&body='+body; 
    
    win = window.open(mailto_link,'emailWindow'); 
    if (win && win.open &&!win.closed) win.close(); 
    }
    The problem is that even if the validation comes back with missing fields the second scrip will still generate a cleint email, minus the missing information. The validation error box pops up behind the newly cenerated email. I am trying to make it so if validation says that fields are missing the second script will not run. Any ideas how to do this?

  2. #2
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    Well, nothing's really jumping out at me, and you have not shown how you are invoking the formCheck and emailForm functions (In the future don't leave key things like that out -you are very lucky to get a response at all without that info). But I guess if it were me, I would probably do something along the lines of: <form action="yadayadayada.php" method="post" onsubmit="return formCheck(this);"> or I would set it via script to the form object, whatever. Then I would insert into the formCheck function in the final 'if' statement:
    Code:
     if (alertMsg.length == l_Msg){
                    emailForm(formobj); //pass the reference to the form, lighten the code in emailForm a bit..
      return true;
     }
    And there is really no need that I know of for the following in the emailForm function:
    win = window.open(mailto_link,'emailWindow');
    if (win && win.open &&!win.closed) win.close();
    Could replace that with:
    Code:
    window.location.href = mailto_link;
    Just my observations, hope it helps.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles