two functions on one page

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
 
D

David Dorward

Phil said:
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.
return validate()
return hide_input()

When you return from a function, that function stops executing. Returning is
saying "OK, I have the answer. Pass the answer back to whatever called me,
then stop".
 
P

Phil Thompson

David said:
When you return from a function, that function stops executing. Returning
is
saying "OK, I have the answer. Pass the answer back to whatever called me,
then stop".

ah I see! I knew it would be obvious. It now works as I've changed it so the
return hide_input() has been moved to the validate function's else
statement and it runs before the return true statement. e.g.

else{
hide_input()
return true;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,082
Latest member
KetonaraKetoACV

Latest Threads

Top