Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JS Validation and Form Submission 1

Status
Not open for further replies.

bezking

IS-IT--Management
Jun 14, 2007
38
US
Hi Guys,
I am stumped by this javascript. It is supposed to validate and submit a form. When I click submit with fields left out, it throws the error I set. However, when everything is right the form won't submit. How do I fix this? THANKS!

Code:
function validate()
{
var error=document.getElementById("errors")
var okay=document.getElementById("okay")
var form=document.getElementById("form1")
var text=document.getElementById("text")
var freq=document.getElementById("freq")
var call=document.getElementById("callsign")
var msg=document.getElementById("msg")
var btn=document.getElementById("submit")
good="yes"
if (text.value.length<3)
{
alert("text error")
text.className="formError"
good="no"
}
if (freq.value.length<4)
{
alert("frequency error")
freq.className="formError"
good="no"
}
if (call.value.length<2)
{
alert("callsign error")
call.className="formError"
good="no"
}
if (good=="yes")
{
form.submit()
}
}
 
[1]
>var btn=document.getElementById("submit")
You mean you assign an id to the submit button with the value "submit". Change the id to anything other than "submit" - it is no good. This is the single severe error here.
[2] btn is not needed in the function, so you can further leave it out.
[3] Furthermore, you declare many variable, but not on the "good", you should do the same on it for better.
[tt]var good="yes"[/tt]
[4] The function validate() as it stands should _not_ be the onsubmit handler - I hope not. You don't do that for onsubmit handler. If it is onsubmit handler, the last part of the function should be scripted otherwise.
[tt]
if (good=="yes")
{
return true;
} else {
return false;
}
[/tt]
and the form1 element looks like this.

[tt]<form name="form1" ... onsubmit="return validate()">[/tt]

But you may well have made good use of the function in the form shown other than onsubmit handler - that info is missing to start with.
 
THANKS SOOOOOOO MUCH! It was perfect. Thanks for the help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top