/ Published in: JavaScript
This is my complete code for contact.js
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function validateFields() { var frmEl = document.getElementById("cForm"); var posRegard = document.getElementById("posRegard"); var posName = document.getElementById("posName"); var posEmail = document.getElementById("posEmail"); var posText = document.getElementById("posText"); var strCC = document.getElementById('selfCC'); var whiteSpace = /^[\s]+$/; var emailFormat = /^[\w\.\-]+@[\w\.\-]+\.[\w\.\-]+$/; if ( posRegard.value == "" || whiteSpace.test(posRegard.value) ) { document.getElementById("emailSuccess").style.display = "none"; hideAllErrors(); document.getElementById("regardError").style.display = "inline"; document.getElementById("posRegard").select(); document.getElementById("posRegard").focus(); } else if ( posName.value == "" || whiteSpace.test(posName.value) ) { document.getElementById("emailSuccess").style.display = "none"; hideAllErrors(); document.getElementById("nameError").style.display = "inline"; document.getElementById("posName").select(); document.getElementById("posName").focus(); } else if ( posEmail.value == "" || whiteSpace.test(posEmail.value) ) { document.getElementById("emailSuccess").style.display = "none"; hideAllErrors(); document.getElementById("emailError1").style.display = "inline"; document.getElementById("posEmail").select(); document.getElementById("posEmail").focus(); } else if ( (posEmail.value !="") && (!posEmail.value.match(emailFormat)) ) { document.getElementById("emailSuccess").style.display = "none"; hideAllErrors(); document.getElementById("emailError2").style.display = "inline"; document.getElementById("posEmail").select(); document.getElementById("posEmail").focus(); } else if ( posText.value == "" || whiteSpace.test(posText.value) ) { document.getElementById("emailSuccess").style.display = "none"; hideAllErrors(); document.getElementById("messageError").style.display = "inline"; document.getElementById("posText").select(); document.getElementById("posText").focus(); } else { hideAllErrors(); sendPosEmail(); } } function hideAllErrors() { document.getElementById("regardError").style.display = "none"; document.getElementById("nameError").style.display = "none"; document.getElementById("emailError1").style.display = "none"; document.getElementById("emailError2").style.display = "none"; document.getElementById("messageError").style.display = "none"; document.getElementById("ccError").style.display = "none"; } function sendPosEmail () { var success = document.getElementById('emailSuccess'); var posRegard = document.getElementById('posRegard'); var posName = document.getElementById('posName'); var posEmail = document.getElementById('posEmail'); var posText = document.getElementById('posText'); var strCC = document.getElementById('selfCC').value; var page = "scripts/xmlHttpRequest.php?contact=true&xml=true"; showContactTimer(); // quickly begin the load bar success.style.display = 'none'; // hide the success bar (incase this is a multi-email document.getElementById("sendContactEmail").disabled = true; //disable the Send button // convert (&, +, =) to string equivs. Needed so URL encoded POST won't choke. var str1 = posName.value; str1 = str1.replace(/&/g,"**am**"); str1 = str1.replace(/=/g,"**eq**"); str1 = str1.replace(/\+/g,"**pl**"); var str2 = posEmail.value; str2 = str2.replace(/&/g,"**am**"); str2 = str2.replace(/=/g,"**eq**"); str2 = str2.replace(/\+/g,"**pl**"); var str4 = posText.value; str4 = str4.replace(/&/g,"**am**"); str4 = str4.replace(/=/g,"**eq**"); str4 = str4.replace(/\+/g,"**pl**"); var stuff = "selfCC="+strCC+"&posRegard="+str1+"&posName="+str1+"&posEmail="+str2+"&posText="+str4; loadXMLPosDoc(page,stuff) } function showContactTimer () { var loader = document.getElementById('loadBar'); loader.style.display = 'block'; sentTimer = setTimeout("hideContactTimer()",6000); } function hideContactTimer () { var loader = document.getElementById('loadBar'); var success = document.getElementById('emailSuccess'); var fieldArea = document.getElementById('contactFormArea'); var inputs = fieldArea.getElementsByTagName('input'); var inputsLen = inputs.length; var tAreas = fieldArea.getElementsByTagName('textarea'); var tAreasLen = tAreas.length; // Hide the load bar alas! Done Loading loader.style.display = "none"; success.style.display = "block"; success.innerHTML = grabPosXML("confirmation"); document.getElementById("sendContactEmail").disabled = false; //enable the Send button // Now Hijack the form elements for ( i=0;i<inputsLen;i++ ) { if ( inputs[i].getAttribute('type') == 'text' ) { inputs[i].value = ''; } } for ( j=0;j<tAreasLen;j++ ) { tAreas[j].value = ''; } } function ajaxContact() { var frmEl = document.getElementById('cForm'); addEvent(frmEl, 'submit', validateFields, false); frmEl.onsubmit = function() { return false; } } addEvent(window, 'load',ajaxContact, false); function toggle(targetId) { target = document.getElementById(targetId); if (target.style.display == 'block'){ target.style.display='none'; } else { target.style.display='block'; } }