473,378 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

New user- function doesn't work

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

Jul 23 '05 #1
7 1209
kdc wrote:
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)">

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #2
Randy Webb wrote:
kdc wrote:
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";}


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...

--
Rob
Jul 23 '05 #3
kdc


RobG wrote:
Randy Webb wrote:
kdc wrote:
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";}


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...

--
Rob


Jul 23 '05 #4
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!

Jul 23 '05 #5
kdc wrote:
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?
Jul 23 '05 #6
Lee
Randy Webb said:

kdc wrote:
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.


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.

Jul 23 '05 #7
kdc wrote:
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
Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jesper Stocholm | last post by:
I have implemented role-based security within my ASP.Net application. However, it seems the role is not passed to the authentication ticket I create. I want to use it to display/hide some...
5
by: Michelle Stone | last post by:
Hi everybody I am writing a simple asp.net application using form authentication. I store the list of all users and their passwords in an SQL Server database table. My client recently told me...
1
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a...
3
by: Jiho Han | last post by:
Should ASPNET user belong to the local Users group? I may have made some changes that affected my workstation setup and I am experiencing some unexpected behaviors. For example, I have my IIS...
4
by: Guadala Harry | last post by:
Is there any way for one Session to remove and update objects in another Session? I seriously doubt it, but thought I'd ask. Here's why: I have some data that is unique per user (or per session -...
17
by: Alphonse Giambrone | last post by:
I am building a web app for users to add/edit data. They may add/edit several records during a session. When they are done (not necessarily immediately, could be 10 or more minutes later), I need...
13
by: Michael | last post by:
I have setup a public variable in the Master Page "code-behind-file". Now I would like to set that value from the UserControl, but I can't seem to find a way to do this. Does anyone have any ideas?...
1
by: Carlettus | last post by:
Dear All, sorry but I'm not sure if this is the right place to post my problem. I was using the following asp code to create users in Active Directory. Suddenly, and I don't know the reason, users...
0
by: rbukkara | last post by:
Hi, I have got the following error while trying to add a user in the LDAP Directory. javax.naming.NameNotFoundException: ; remaining name 'uid=vassila,ou=People,dc=cs,dc=uno,dc=edu' I have...
3
by: shapper | last post by:
Hello, On my web site I have a property, Visitor, which is available for Anonymous users: public class Visitor { public CultureInfo Culture { get; set; } public List<GuidPolls { get; set;...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.