P
Phil Thompson
Hi. I'm learning JavaScript slowly and I'm trying to get a form validation
to work with a usability feature (hiding the input button on submit)
whichever function i chose to run first onsubmit is the only one that runs.
I'm sure this is a simple answer but, I've not found it yet. Otherwise can
anyone comment on whether this code has any glaring problems with it? my
other problem is that this code grounds to a halt if one of the forms
variables isn't present on a particular page meaning I need a different
script for each page, which obviosuly isn't very acceptable.
p.s. I understand that window.onload isn't the most standards-based way to
do this but, is the most cross-browser effective (I think).
heres the code:
// Form validation
window.onload = function()
{
if (document.getElementsByTagName){
document.forms[1].onsubmit = function () {
return validate()
return hide_input()
}
}
}
function hide_input(){
var hide_button = document.forms[1].getElementsByTagName('input');
for (var i=0;i<hide_button.length;i++)
{
if(hide_button.className == 'button')
{
hide_button.style.display = 'none';
}
}
var show_wait_sign = document.forms[1].getElementsByTagName('p');
for (var i=0;i<show_wait_sign.length;i++)
{
if(show_wait_sign.className == 'wait_sign')
{
show_wait_sign.style.display = 'block';
}
}
}
function validate(){
x = document.forms[1]
email = x.email.value
name = x.sender.value
subject = x.title.value
comment = x.message.value
if(email == ""){
alert("You've forgotten to include your email address. We need this incase
we need to contact you regarding your comment.");
return false;
}
if(comment == "" || comment == " "){
alert("You haven't entered a message!");
return false;
}
if(subject == ""){
alert("You haven't entered a subject. How will we know what your message
is about?");
return false;
}
if(name == ""){
alert("You haven't entered your name. How will we know who has contacted
us?");
return false;
}
else{
return true;
}
}
// end of script
to work with a usability feature (hiding the input button on submit)
whichever function i chose to run first onsubmit is the only one that runs.
I'm sure this is a simple answer but, I've not found it yet. Otherwise can
anyone comment on whether this code has any glaring problems with it? my
other problem is that this code grounds to a halt if one of the forms
variables isn't present on a particular page meaning I need a different
script for each page, which obviosuly isn't very acceptable.
p.s. I understand that window.onload isn't the most standards-based way to
do this but, is the most cross-browser effective (I think).
heres the code:
// Form validation
window.onload = function()
{
if (document.getElementsByTagName){
document.forms[1].onsubmit = function () {
return validate()
return hide_input()
}
}
}
function hide_input(){
var hide_button = document.forms[1].getElementsByTagName('input');
for (var i=0;i<hide_button.length;i++)
{
if(hide_button.className == 'button')
{
hide_button.style.display = 'none';
}
}
var show_wait_sign = document.forms[1].getElementsByTagName('p');
for (var i=0;i<show_wait_sign.length;i++)
{
if(show_wait_sign.className == 'wait_sign')
{
show_wait_sign.style.display = 'block';
}
}
}
function validate(){
x = document.forms[1]
email = x.email.value
name = x.sender.value
subject = x.title.value
comment = x.message.value
if(email == ""){
alert("You've forgotten to include your email address. We need this incase
we need to contact you regarding your comment.");
return false;
}
if(comment == "" || comment == " "){
alert("You haven't entered a message!");
return false;
}
if(subject == ""){
alert("You haven't entered a subject. How will we know what your message
is about?");
return false;
}
if(name == ""){
alert("You haven't entered your name. How will we know who has contacted
us?");
return false;
}
else{
return true;
}
}
// end of script