...

View Full Version : Script stop working when bringing in 2 new entities



FlashDance
09-22-2011, 03:28 AM
This code was working fine until I added "["lastname"]["email"]" to it. Why has this problem developed? Was I suppose to separate them or something?


<SCRIPT type="text/javascript">
function validateForm() {
var x=document.forms["form"]["firstname"]["lastname"]["email"].value;
if (x=="") {
alert ("Fields required for submission have not been filled.");
return false;
}
}
</SCRIPT>
<FORM action="action.php" method="post" name="form" onsubmit="return validateForm()">

<INPUT name="firstname" type="text">
<BR>
<INPUT name="lastname" type="text">
<BR>
<INPUT name="email" type="text">
<BR>
<INPUT class="submit" type="submit" value="Submit">
</FORM>

Desmondowq
09-22-2011, 03:52 AM
Hi, i'm still new to javascript but i think the problem is at
var x=document.forms["form"]["firstname"]["lastname"]["email"].value;


I believe there is a better way of doing this

if (document.forms["form"]["firstname"].value=="" || document.forms["form"]["lastname"].value=="" || document.forms["form"]["email"].value=="" ) {
alert ("Fields required for submission have not been filled.");
return false;
}

Old Pedant
09-22-2011, 04:51 AM
Yes.

But you can do it much shorter, too:


function validateForm(form)
{
if ( form.firstname.value=="" || form.lastname.value=="" || form.email.value=="" )
{
alert ("Fields required for submission have not been filled.");
return false;
}
return true; // really should do this
}
...
<form action="action.php" method="post" onsubmit="return validateForm(this)">
...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum