not sure what is stopping my code from running

B

brian

i broke down where i think the problems areas would be.
any help would be greatly appreciated.

where the file is called
<script type="text/javascript" src="k.js">
</script>


the beginning of my form, test.cgi doesnt exsist but i think it should
run the function before the action. Is this correct???
<form name="courseform" action="test.cgi" method="post"
onsubmit="return checkForm(this);">

then my code in the file

not sure if it would be better to do the getElementById or just
theform.last_name.value
I havent finished the script but i think you get the idea.

i know the long number are a bit off the wall but i have to use them
sadly.

well thank you for looking over my code.
brian
<script>

function checkForm(theForm)
{
alert("yes");
var error = "";
error += checkFname(theForm..getElementById('first_name').value);
error += checkLname(theForm.last_name.value);
error += checkPhone(theForm.phone.value);
error += checkEmail(theForm.email.value);
error += checkCntmethod(theForm.00N40000001U1MW.selectedIndex);
error += checkCnttime(theForm.00N40000001U1Mm.value);
error += checkAgency(theForm.company.value);
error += checkAtype(theForm.00N40000001U1MR.selectedIndex);
error += checkAaddress(theForm.street.value);
error += checkAcity(theForm.city.value);
error += checkAcounty(theForm.00N30000001E9CN.value);
error += checkAzip(theForm.zip.value);
error += checkCrsname(theForm.00N40000001U1MN.selectedIndex);
error += checklanguage(theForm.00N40000001U2Z8.selectedIndex);
error += checkPdate(theForm.00N40000001U1Mx.value);
error += checkPtime(theForm.00N40000001U1NF.selectedIndex);
error += checkAdate(theForm.00N40000001U1Li.value);
error += checkAtime(theForm.00N40000001U1Lo.selectedIndex);
error += isEmpty(theForm.notempty.value);
error += isDifferent(theForm.different.value);

if (error != "") {
alert(error);
return false;
}
return false;
}

//checks the first name for null statement and length
function checkFname(fname){
var errror = "";
if(fname ==""){
error = "You did not enter a first name. /n"
}
else if (fname.length < 2 || fname.length > 15){
error = "The name entered is incorrect. /n"
}
return error;
}

//checks last name for null statement
function checkLname(lname){
var errror = "";
if(lname ==""){
error = "You did not enter a last name. /n"
}
return error;
}

function checkPhone(phone){
var errror = "";
if(phone ==""){
error = "You did not enter a phone number. /n"
}
else if(phone.length == 10){
error = "Your phone number is the incorrect length. /n"
}
return error;
}

function checkEmail(email){
var errror = "";
if(email ==""){
error = "You did not enter an email address. /n"
}
return error;
}

//checks the contact method
function checkCntmethod(choice) {
var error = "";
if (choice == 0) {
error = "You did not choose a Best cotact method.\n";
}
return error;
}

function checkCnttime(conTime){
var errror = "";
if(conTime ==""){
error = "You did not enter a contact time. /n"
}
return error;
}

function checkAgency(agency){
var errror = "";
if(agency ==""){
error = "You did not enter your agency's name. /n"
}
return error;
}

function checkAtype(choice) {
var error = "";
if (choice == 0) {
error = "You did not choose an agency type.\n";
}
return error;
}

function checkAaddress(address){
var errror = "";
if(adress ==""){
error = "You did not enter the agency's address. /n"
}
return error;
}

function checkAcity(city){
var errror = "";
if(city ==""){
error = "You did not enter the agency's city. /n"
}
return error;
}

function checkAcounty(county){
var errror = "";
if(county ==""){
error = "You did not enter the agency's city. /n"
}
return error;
}
</script>
 
W

web.dev

brian said:

In your external javascript file, you do not need the script tag.
function checkForm(theForm)
{
alert("yes");
var error = "";
error += checkFname(theForm..getElementById('first_name').value);

There's a typo. You have two '.', but even then, is the
getElementById() method necessary?

[snip]
if (error != "") {
alert(error);
return false;
}
return false;
}

This may be problematic. You're saying, if there's a error, alert the
messages and do *not* submit the form. However, at the end you're also
saying, do *not* submit the form. You probably meant to return true at
the end, thus submitting the form to the action that you've placed.

[snip]
 
R

Randy Webb

web.dev said the following on 10/24/2006 6:32 PM:
In your external javascript file, you do not need the script tag.


There's a typo. You have two '.', but even then, is the
getElementById() method necessary?

It is not only un-necessary but it will throw an error as FORM elements
do not have a method of getElementById - it belongs to the document
alone. Not even the - window - object can use it.
 
A

ASM

brian a écrit :
the beginning of my form, test.cgi doesnt exsist but i think it should
run the function before the action. Is this correct???
yeap

<form name="courseform" action="test.cgi" method="post"
onsubmit="return checkForm(this);">

then my code in the file

not sure if it would be better to do the getElementById or just
theform.last_name.value

certainly not doing :
checkFname(theForm..getElementById('first_name').value);
--------------------^^
only one point please !

Your function checkForm() is out on its first line :-(

Anyway it is always better to address to elements of a form
by the tree of forms :

var elt =document.forms['myForm'].elements['myElement']
or
var elt =document.myForm.myElement
or
var f = document.forms['myForm'];
var elt = f.myElement;

var val = elt.value
<script>

function checkForm(theForm)
{
alert("yes");
var error = "";
error += checkFname(theForm..getElementById('first_name').value);
|
here ------------------------------+
error += checkLname(theForm.last_name.value); (snip
if (error != "") {
alert(error);
return false;
}
return false;

no ! return true; (except if it was for your tests)
}


else if(phone.length == 10){
error = "Your phone number is the incorrect length. /n"

I'm sorry not in France !

and what you do with spaces ?
 
R

RobG

brian said:
i broke down where i think the problems areas would be.
any help would be greatly appreciated.

In addition to what others have said:

[...]
function checkForm(theForm)
{
alert("yes");
var error = "";
error += checkFname(theForm..getElementById('first_name').value);
error += checkLname(theForm.last_name.value);
error += checkPhone(theForm.phone.value);

The += compound operator is very slow in at least one popular browser,
you may find it quicker to concatenate results:

var error =
checkFname(theForm..getElementById('first_name').value)
+ checkLname(theForm.last_name.value)
+ checkEmail(theForm.email.value)
+ ...

error += checkEmail(theForm.email.value);
error += checkCntmethod(theForm.00N40000001U1MW.selectedIndex);

To be valid, id and name attributes must start with a letter.

[...]
if (error != "") {
alert(error);

Your users may find it more helpful if you write errors to the page,
that way they can still see the message while they fix them. Using an
alert, they have to remember what the errors were - and they may get 20
of them.
return false;
}
return false;

As Steph said, that should be true.
}

//checks the first name for null statement and length
function checkFname(fname){
var errror = "";
if(fname ==""){
error = "You did not enter a first name. /n"
}

Given that the local variable is called 'errror', this will create a
global variable 'error' and give it a value, but only if the function
is called. That may cause unexpected and difficult to find problems
elsewhere.

You have multiple instances of this error. It can be avoided by
removing the local 'error' variable and just returning the message:

function checkFname(fname) {
if (fname =="") {
return "You did not enter a first name. /n"
} else if (fname.length < 2 || fname.length > 15){
return "The name entered is incorrect. /n"
}
}

The advice returned by the else branch doesn't seem to fit the test,
you should say something like "First name must be from 2 to 15
characters inclusive".

//checks last name for null statement

It checks to see if the value is an empty string - null is a special
value in javascript.
function checkLname(lname){
var errror = "";
if(lname ==""){
error = "You did not enter a last name. /n"
}
return error;
}
[...]
function checkAcounty(county){
var errror = "";
if(county ==""){
error = "You did not enter the agency's city. /n"

Or maybe the county?


There may be other errors...
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top