Hiya!
I’m a newbie in the field of js, just finished my first website, still in volunteering.
OK, my problem is: I decided to apply some client side validation of the site’s forms. The following script will probably trigger some indignation amongst you, but this is my first script, practically copy-pasted from snippets I found on the web.
The first few lines of the script is the beginning of the validation function, interrupted by the definition of an alert customizing function. My first question is exactly about the place: did I well place this customizing thing, or could you suggest me a better way to place it?
After this definition within a definition, the validation script continues and calls/defines many times the removeCustomAlert function. That’s because of my second problem: originally, without the alert-customizing function, after the standard alert box had been closed, the related form field got focused. But the custom function stopped working after that the customized alert box had been applied, so finally I replaced the focus() function, right inside the removeCustomAlert function. That was the only way I could solve the issue, but gosh, it’s many many code. So my second question is: do you know an easier way to code this whole stuff?
Thanks in advance for all help and suggestions.
So, the code is:
[FONT=”Courier New”][SIZE=”2″][INDENT][INDENT]function checkForm() {
var cname, cemail, cfname, cfemail, cmessage;
with(window.document.frdForm)
{
cname = yourname;
cemail = yourmail;
cfname = friendsname;
cfemail = friendsmail;
cmessage = message; }
var ALERT_TITLE = “Oops!”;
var ALERT_BUTTON_TEXT = “Close”;
if(document.getElementById) {
window.alert = function(txt) {
createCustomAlert(txt);
}
}
function createCustomAlert(txt) {
d = document;
if(d.getElementById(“modalContainer”)) return;
mObj = d.getElementsByTagName(“body”)[0].appendChild(d.createElement(“div”));
mObj.id = “modalContainer”;
mObj.style.height = document.documentElement.scrollHeight + “px”;
alertObj = mObj.appendChild(d.createElement(“div”));
alertObj.id = “alertBox”;
if(d.all && !window.opera) alertObj.style.top = document.documentElement.scrollTop + “px”;
alertObj.style.left = (d.documentElement.scrollWidth – alertObj.offsetWidth)/2 + “px”;
h1 = alertObj.appendChild(d.createElement(“h1”));
h1.appendChild(d.createTextNode(ALERT_TITLE));
msg = alertObj.appendChild(d.createElement(“p”));
msg.innerHTML = txt;
btn = alertObj.appendChild(d.createElement(“a”));
btn.id = “closeBtn”;
btn.appendChild(d.createTextNode(ALERT_BUTTON_TEXT));
btn.href = “#”;
btn.onclick = function() { removeCustomAlert();return false; }
}
if(trim(cname.value) == ”)
{
alert(‘Please enter your name’);
function removeCustomAlert() {document.getElementsByTagName(“body”)[0].removeChild(document.getElementById(“modalContainer”)); cname.focus();}
return false;
}
else if(trim(cemail.value) == ”)
{
alert(‘Please enter your email’);
function removeCustomAlert() {document.getElementsByTagName(“body”)[0].removeChild(document.getElementById(“modalContainer”)); cemail.focus();}
return false;
}
else if(!isEmail(trim(cemail.value)))
{
alert(‘Email address is not valid’);
function removeCustomAlert() {document.getElementsByTagName(“body”)[0].removeChild(document.getElementById(“modalContainer”)); cemail.focus();}
return false;
}
else if(trim(cfname.value) == ”)
{
alert(‘Please enter your friend’s name’);
function removeCustomAlert() {document.getElementsByTagName(“body”)[0].removeChild(document.getElementById(“modalContainer”)); cfname.focus();}
return false;
}
else if(trim(cfemail.value) == ”)
{
alert(‘Please enter your friend’s email’);
function removeCustomAlert() {document.getElementsByTagName(“body”)[0].removeChild(document.getElementById(“modalContainer”)); cfemail.focus();}
return false;
}
else if(!isEmail(trim(cfemail.value)))
{
alert(‘Email address is not valid’);
function removeCustomAlert() {document.getElementsByTagName(“body”)[0].removeChild(document.getElementById(“modalContainer”)); cfemail.focus();}
return false;
}
else if(trim(cmessage.value) == ”)
{
alert(‘Please enter your message’);
function removeCustomAlert() {document.getElementsByTagName(“body”)[0].removeChild(document.getElementById(“modalContainer”)); cmessage.focus();}cmessage.focus();
return false;
}
else
{
cname.value = trim(cname.value);
cemail.value = trim(cemail.value);
cfname.value = trim(cfname.value);
cfemail.value = trim(cfemail.value);
cmessage.value = trim(cmessage.value);
return true;
}
}
function trim(str)
{
return str.replace(/^s+|s+$/g,”);
}
function isEmail(str)
{
var regex = /^[-_.a-z0-9]+@(([-..
return regex.test(str);
} [/INDENT][/INDENT][/SIZE][/FONT]