New user- function doesn't work

K

kdc

Hello,
I would appreciate any help I can get from anyone who has experience
using javascript. I have an html form containing a number of functions
controlling the submission of data to our database. I now want to
require users to fill in three specific fields before they can submit
the form. I found a function and modified it with my form name
(mainform), and field names(sub 1,2 and 3)
In the head of the document, I added this function to the existing ones
that work successfully:

function required()
{
if (document.mainform.sub1.value==" ")
{
alert("Please Enter Name!");
document.mainform.sub1.focus()
return false;}
return true;

if (document.mainform.sub2.value==" ")
{
alert("Please Enter Position");
document.mainform.sub2.focus()
return false;}
return true;

if (document.mainform.sub3.value==" ")
{
alert("Please Enter Email Address");
document.mainform.sub3.focus()
return false;}
return true;
}

In the body I have

form name ="=mainform"

The other functions that work prevent submission under certain
conditions, but my new function is just ignored. Does anyone have any
suggestions?
Thanks, kdc
 
R

Randy Webb

kdc said:
Hello,
I would appreciate any help I can get from anyone who has experience
using javascript. I have an html form containing a number of functions
controlling the submission of data to our database. I now want to
require users to fill in three specific fields before they can submit
the form. I found a function and modified it with my form name
(mainform), and field names(sub 1,2 and 3)
In the head of the document, I added this function to the existing ones
that work successfully:

function required()
{
if (document.mainform.sub1.value==" ")

That is checking to see if sub1's value is a space. Not what I think you
want.

if (document.mainform.sub1.value == '')
{
alert("Please Enter Name!");
document.mainform.sub1.focus()
return false;}
return true;

Right here, the function is either going to return true or false, any
following code will never be executed.
if (document.mainform.sub2.value==" ")
{
alert("Please Enter Position");
document.mainform.sub2.focus()
return false;}
return true;

if (document.mainform.sub3.value==" ")
{
alert("Please Enter Email Address");
document.mainform.sub3.focus()
return false;}
return true;
}

function required(formRef){
var alertMessage = '';
if (formRef.sub1.value = ''){alertMessage += "Name Required\n";}
if (formRef.sub2.value = ''){alertMessage += "Position Required\n";}
if (formRef.sub3.value = ''){alertMessage += "Email Required\n";}
if (alertMessage == ''){return true;}
else{alert(alertMessage);return false;}
}

In the body I have

form name ="=mainform"

<form name="mainform" onsubmit="return required(this)">
 
R

RobG

Randy said:
That is checking to see if sub1's value is a space. Not what I think you
want.

if (document.mainform.sub1.value == '')



Right here, the function is either going to return true or false, any
following code will never be executed.



function required(formRef){
var alertMessage = '';
if (formRef.sub1.value = ''){alertMessage += "Name Required\n";}

A good argument for always putting literals on the left when doing
evaluations (unless assignment is actually intended). In this
case, the value of formRef.sub1.value is set to '' and the test is
always true (unless the assignment is 'fixed' by older browsers) and
the content of the form element is effectively deleted. Try:

if ( '' = formRef.sub1.value ){alertMessage += "Name Required\n";}

will return an error, prompt fixing:

if ( '' == formRef.sub1.value ){alertMessage += "Name Required\n";}
if (formRef.sub2.value = ''){alertMessage += "Position Required\n";}
if (formRef.sub3.value = ''){alertMessage += "Email Required\n";}

ditto...
 
K

kdc

RobG said:
A good argument for always putting literals on the left when doing
evaluations (unless assignment is actually intended). In this
case, the value of formRef.sub1.value is set to '' and the test is
always true (unless the assignment is 'fixed' by older browsers) and
the content of the form element is effectively deleted. Try:

if ( '' = formRef.sub1.value ){alertMessage += "Name Required\n";}

will return an error, prompt fixing:

if ( '' == formRef.sub1.value ){alertMessage += "Name Required\n";}


ditto...
 
K

kdc

Thanks to both of you for your quick feedback.
This is what I added to my program:

function required(mainform){
var alertMessage = '';
if (' '==mainform.sub1.value ){alertMessage += "Name Required\n";}
if (' '==mainform.sub2.value){alertMessage += " Position Required\n";}
if (''==mainform.sub3.value){alertMessage += "Email Required\n";}
if (alertMessage == ''){return true;}
else{alert(alertMessage);return false;}

}


I wasn't sure whether to use =mainform or ==mainform so I also tried it
with a single =. It continues to ignore it.

I don't want to waste your time so let me ask if the fact that I call
the function by
adding it to another function (checkValues) that is then called in upon
submission matters.

I added required(this) at the end of the list as below:
function checkValues() {
if (! ( checkMonths() && checkTimes() &&
checkRoutes1() &&
checkAges() && checkGenders() &&
checkVars() && checkCauses() && checkPhases() &&
checkPersons() &&
checkFactors() && checkTypes() && checkErrors() &&
required(this) ) ) {
return false;
}
}

I also tried it without the 'this' just in case.

In the body:

<FORM NAME="mainForm" onsubmit="return checkValues();"
ACTION="readfile.php" method="POST">

Thanks again for your help!
 
R

Randy Webb

kdc said:
Thanks to both of you for your quick feedback.
This is what I added to my program:

function required(mainform){
var alertMessage = '';
if (' '==mainform.sub1.value ){alertMessage += "Name Required\n";}

You are checking to see if sub1 is a space, not what you wanted.
if (' '==mainform.sub2.value){alertMessage += " Position Required\n";}

Same there.
if (''==mainform.sub3.value){alertMessage += "Email Required\n";}
if (alertMessage == ''){return true;}
else{alert(alertMessage);return false;}

}


I wasn't sure whether to use =mainform or ==mainform so I also tried it
with a single =. It continues to ignore it.

It should be a double ==. My original had the typo in it and got
propogated by me copy/pasting it.
I don't want to waste your time so let me ask if the fact that I call
the function by
adding it to another function (checkValues) that is then called in upon
submission matters.

Yes, it makes a huge difference.
I added required(this) at the end of the list as below:

"this", in that context, refers to the function, not the form. But since
you don't use the parameter, theres no point in adding it.
function checkValues() {
if (! ( checkMonths() && checkTimes() &&
checkRoutes1() &&
checkAges() && checkGenders() &&
checkVars() && checkCauses() && checkPhases() &&
checkPersons() &&
checkFactors() && checkTypes() && checkErrors() &&
required(this) ) ) {
return false;
}
}

I also tried it without the 'this' just in case.

Add some alerts in your functions to see where you are making it to so
that you can find out where its stopping.
In the body:

<FORM NAME="mainForm" onsubmit="return checkValues();"
ACTION="readfile.php" method="POST">

Thanks again for your help!


--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Answer:It destroys the order of the conversation
Question: Why?
Answer: Top-Posting.
Question: Whats the most annoying thing on Usenet?
 
L

Lee

Randy Webb said:
You are checking to see if sub1 is a space, not what you wanted.


Same there.


It should be a double ==. My original had the typo in it and got
propogated by me copy/pasting it.


Yes, it makes a huge difference.


"this", in that context, refers to the function, not the form. But since
you don't use the parameter, theres no point in adding it.

The argument to required() *is* being used as a reference to the form.
Changing "required(this)" to "required(document.mainform)" looks like
the easiest way to fix it.
 
T

Thomas 'PointedEars' Lahn

kdc said:
Thanks to both of you for your quick feedback.
This is what I added to my program:

function required(mainform){
var alertMessage = ''; ^^^[1]
if (' '==mainform.sub1.value ){alertMessage += "Name Required\n";}
^^^ ^^^^^^^^^^^^^[4] ^^[3]
if (' '==mainform.sub2.value){alertMessage += " Position Required\n";}
^^^^^^^^^^^^^[4] ^^[3]
if (''==mainform.sub3.value){alertMessage += "Email Required\n";}
^^^^^^^^^^^^^[4] ^^[3]
if (alertMessage == ''){return true;} ^^^^^[3]
else{alert(alertMessage);return false;}

}

Which is not only badly formatted and thus hardly legible[1], but will
also allow users to enter empty strings and strings consisting of two
or more spaces or other whitespace in some input elements[2] while
being inefficient[3] and not using standards compliant referencing[4].
Therefore, it should instead read

function required(mainform)
{
var
rxEmpty = new RegExp("^\\s*$"),
es = mainform.elements,
alertMessage = new Array();

if (rxEmpty.test(es["sub1"].value))
{
alertMessage.push("Name Required");
}

if (rxEmpty.test(es["sub2"].value))
{
alertMessage.push("Position Required");
}

if (rxEmpty.test(es["sub3"].value))
{
alertMessage.push("Email Required");
}

return (alertMessage.length == 0 || !!alert(alertMessage.join("\n"));
}
I wasn't sure whether to use =mainform or ==mainform so I also tried it
with a single =. It continues to ignore it.

It is not ignored but as you try to assign (`=') something to a (string)
literal (' ' and ''), it causes a script error; with my UA:

| SyntaxError: invalid assignment left-hand side

But you have obviously either disabled the display of script errors or not
had a look into the UA's JavaScript console.
I don't want to waste your time [...]

Great. Why do you not read then previous discussions on the subject before
you post?


PointedEars
 

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
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top