473,395 Members | 2,079 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,395 software developers and data experts.

checking form fields

Hi!
I've got problem.
Everything works fine when on line !1! is just single entry in if
statement. Why after putting "&&" script doesn't work?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>

function checkAns( form )
{
var radiock;
for(var i = 0; i < form.radio1.length; i++)
{
if( form.radio1[i].checked )
{
radiock = form.radio1[i].value;
}
}
if( (form.forename.value == "") && (radiock == null) ) /* !1! */
{
alert("please answer all compulsory questions!");
return false;
}
else
{
return true;
}
}
</script>

</HEAD>
<BODY>
<form name="myform" method="get" onsubmit="return checkAns(this);">
<P>
Input your name(compulsory): <input type="text" size="40"
name="forename">
<br>
<br>
fav colour(compulsory)?
<br>
<INPUT TYPE="radio" NAME="radio1" VALUE="red">red</P>
<P>
<INPUT TYPE="radio" NAME="radio1" VALUE="blue">blue</P>
<P>
<INPUT TYPE="radio" NAME="radio1" VALUE="green">green</P>
<P>
<INPUT TYPE="radio" NAME="radio1" VALUE="white">white</P>
<P>
marital status <INPUT TYPE="checkbox" name="marital" value=""></P>
<P>
any other comments <TEXTAREA NAME="comment" ROWS="5" COLS="15"
WRAP="physical"></TEXTAREA>
</P>
<P>
<INPUT TYPE="submit" NAME="NAME" VALUE="submit" ID="Submit1">
</P>
</BODY>
</HTML>

any ideas?

--
Bremse
Jul 23 '05 #1
11 1371
hey, look down for my solution...

"Bremse" <br*************@wp.pl> wrote in message
news:l0********************************@4ax.com...
Hi!
I've got problem.
Everything works fine when on line !1! is just single entry in if
statement. Why after putting "&&" script doesn't work?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>

function checkAns( form )
{
var radiock;
for(var i = 0; i < form.radio1.length; i++)
{
if( form.radio1[i].checked )
{
radiock = form.radio1[i].value;
}
}
if( (form.forename.value == "") && (radiock == null) ) /* !1! */
Here you want to use || rather than && or the form can be submitted with an
empty name if a colour has been chosen
{
alert("please answer all compulsory questions!");
return false;
}
else
{
return true;
}
}
</script>

</HEAD>
<BODY>
<form name="myform" method="get" onsubmit="return checkAns(this);">
rather than sending 'this', send 'myform'.
<P>
Input your name(compulsory): <input type="text" size="40"
name="forename">
<br>
<br>
fav colour(compulsory)?
<br>
<INPUT TYPE="radio" NAME="radio1" VALUE="red">red</P>
<P>
<INPUT TYPE="radio" NAME="radio1" VALUE="blue">blue</P>
<P>
<INPUT TYPE="radio" NAME="radio1" VALUE="green">green</P>
<P>
<INPUT TYPE="radio" NAME="radio1" VALUE="white">white</P>
<P>
marital status <INPUT TYPE="checkbox" name="marital" value=""></P>
<P>
any other comments <TEXTAREA NAME="comment" ROWS="5" COLS="15"
WRAP="physical"></TEXTAREA>
</P>
<P>
<INPUT TYPE="submit" NAME="NAME" VALUE="submit" ID="Submit1">
</P>
</BODY>
</HTML>

any ideas?

--
Bremse

Hope that solves your problem!

Phil
Jul 23 '05 #2
Bremse wrote:
[...]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
A title is required, may as well put it here...

<title>Bremse's Form</title>
And for a script to work, you must tell the browser it's a script...
<script type="text/javascript">

function checkAns( form )
{
var radiock;
var radiock = null;
for(var i = 0; i < form.radio1.length; i++)
{
if( form.radio1[i].checked )
{
radiock = form.radio1[i].value;
}
}
if( (form.forename.value == "") && (radiock == null) ) /* !1! */
The extra brackets aren't needed

if(form.forename.value == "" && radiock == null) /* !1! */

The above will submit the form if either "forename" has a value or any
radio button has been selected. It will only fail if both the value is
"" *and* no radio button has been checked, it should be:

if(form.forename.value == "" || radiock == null) /* !1! */

If you are going to evaluate whether radiock is "null", then set it to
null when you declare it. It is actually undefined, but if left
unaltered, the expression will return true as you expect, but just in
case...
{
alert("please answer all compulsory questions!");
return false;
}
I the following is superfluous, if the function completes happily
it will return true anyway.
else
{
return true;
}
}
</script>

</HEAD>
<BODY>
<form name="myform" method="get" onsubmit="return checkAns(this);">
forms also must have an action, even if it's ""

<input .... action="">
<P>
Input your name(compulsory): <input type="text" size="40"
name="forename">
<br>
<br>
fav colour(compulsory)?
<br>
<INPUT TYPE="radio" NAME="radio1" VALUE="red">red</P>
<P>
<INPUT TYPE="radio" NAME="radio1" VALUE="blue">blue</P>
<P>
<INPUT TYPE="radio" NAME="radio1" VALUE="green">green</P>
<P>
<INPUT TYPE="radio" NAME="radio1" VALUE="white">white</P>
<P>
marital status <INPUT TYPE="checkbox" name="marital" value=""></P>
<P>
any other comments <TEXTAREA NAME="comment" ROWS="5" COLS="15"
WRAP="physical"></TEXTAREA>
</P>
<P>
<INPUT TYPE="submit" NAME="NAME" VALUE="submit" ID="Submit1">
</P>
It's good to let the browser know when the form is finished... but
first add a reset button so it's easy for a user to clear the form.

<input type="reset">
</form>
</BODY>
</HTML>


Have fun...

--
Fred
Jul 23 '05 #3
Fred Oz wrote:
[...]

I the following is superfluous, if the function completes happily
it will return true anyway.

That should have been:

The following else is superfluous...
else
{
return true;
}

[...]
forms also must have an action, even if it's ""

<input .... action="">


and that should have been:

<form .... action="">

The joys of imperfection.

--
Fred
Jul 23 '05 #4
On Fri, 26 Nov 2004 10:14:54 +0000 (UTC), Phillip Parr <no@no.com> wrote:
"Bremse" <br*************@wp.pl> wrote in message
news:l0********************************@4ax.com...
<form name="myform" method="get" onsubmit="return checkAns(this);">


rather than sending 'this', send 'myform'.


The OP is correct here. When used in an intrinsic event, the this operator
refers to the element. That particular code is equivalent to

return checkAns(document.forms['myform']);

By the way, replacing either with

return checkAns(myform);

is unreliable and should definitely be avoided.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #5
(...)
Have fun...


thx :-)

--
Bremse
Jul 23 '05 #6
>>> <form name="myform" method="get" onsubmit="return checkAns(this);">

rather than sending 'this', send 'myform'.


The OP is correct here. When used in an intrinsic event, the this operator
refers to the element. That particular code is equivalent to

return checkAns(document.forms['myform']);

By the way, replacing either with

return checkAns(myform);

is unreliable and should definitely be avoided.


Ok, I will change it :-)

--
cheers
Bremse
Jul 23 '05 #7
On Fri, 26 Nov 2004 20:53:09 +1000, Fred Oz <oz****@iinet.net.auau> wrote:

[snip]
The extra brackets aren't needed

if(form.forename.value == "" && radiock == null) /* !1! */
True, but parentheses are good for readability, as much as determining
precedence.

[snip]
if(form.forename.value == "" || radiock == null) /* !1! */
Which could be reduced further to

if(!form.forename.value || !radiock)
If you are going to evaluate whether radiock is "null", then set
it to null when you declare it. It is actually undefined, but if
left unaltered, the expression will return true as you expect, but
just in case...
I would only expect such a comparison to fail with a broken ECMAScript
implementation. The equality algorithm equates undefined and null. Only a
strict equality (===) comparison should evaluate to false.

[snip]
I the following is superfluous, if the function completes happily
it will return true anyway.


A function that exits without any return statement, or with

return;

will return undefined, not true. However, only

return false;

will cancel an event so returning true or undefined is pretty much
equivalent.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #8
On Fri, 26 Nov 2004 11:29:41 +0000, Bremse <br*************@wp.pl> wrote:

[snip]
Ok, I will change it :-)


Ack!! What?

No, *don't* change it. You were right in the first place: use the this
operator!

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #9
While you say the this operator was correct, you don't explain why his
script didn't work. Indeed, I didn't get it to work with 'this' either.

Phil

"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:opsh2ldbfxx13kvk@atlantis...
On Fri, 26 Nov 2004 11:29:41 +0000, Bremse <br*************@wp.pl> wrote:

[snip]
Ok, I will change it :-)


Ack!! What?

No, *don't* change it. You were right in the first place: use the this
operator!

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.

Jul 23 '05 #10
On Fri, 26 Nov 2004 12:03:47 +0000 (UTC), Phillip Parr <no@no.com> wrote:
While you say the this operator was correct, you don't explain why his
script didn't work.


Because I assume that Fred's more comprehensive post was sufficient to
solve the OP's problem. I was merely correcting your misinformation.

Why should I post a solution if someone has already done so?

[snip]

Mike
Please don't top-post.

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #11
sorry, that's my fault for not looking at the times!
"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:opsh2ntttdx13kvk@atlantis...
On Fri, 26 Nov 2004 12:03:47 +0000 (UTC), Phillip Parr <no@no.com> wrote:
While you say the this operator was correct, you don't explain why his
script didn't work.


Because I assume that Fred's more comprehensive post was sufficient to
solve the OP's problem. I was merely correcting your misinformation.

Why should I post a solution if someone has already done so?

[snip]

Mike
Please don't top-post.

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.

Jul 23 '05 #12

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

Similar topics

2
by: Dariusz | last post by:
I have a small form that on button press submits to a PHP file for further processing into a database. I have to change the script as it allows blank / nothing to be submitted to the database. ...
3
by: Antoni | last post by:
Hello I wondered if this script seemed correct? The user should enter a userid and password in two text fields, and then pass these values to the method login_user. Could I ask, is there any...
2
by: Alistair | last post by:
hey peoples. I have a simple form (currently two fields) which is then passed to an ASP page thats stores the data in a dbase I have an error checking script which I can't get to work. the...
3
by: Jack | last post by:
Hi, I have a form when loaded, retrieves record from an access table. Among other fields there is a check box called FinalUpdate. This is tied to a field in Access of type Yes/No. The form...
2
by: Gawie Marais | last post by:
hi all, i have a form and would like to check two fields. if field 1 containes the letter 'c' and the other field contains 'undefined', then an error should apprear ina box on the screen. can...
4
by: reinhout | last post by:
Hi everyone, I have made a script that checks if content is entered in the html boxes and if the required fields are empty, their border lights red. This all works, but if I try to make a...
2
by: fperri | last post by:
Hi, I have a table with over 20 columns. Seven of these of these columns correspond to a filter on a search form. Based on what is selected on the search form, and what is in these seven fields in...
6
by: maxx429 | last post by:
I am trying to check if notes have been added to a Memo field on a form if certain other fields have changed. Example: If the user ticks a particular check box on the form, I want to force them to...
7
by: john.cole | last post by:
I have searched all the groups I can, and I still haven't been able to come up the solution I need. I have the following problem. In my form named sbfrmSpoolList, I am entering a job, spool and...
1
by: geetamadhavi | last post by:
Hi All, I have developed a php applciaiton where a new window is opening on checking the whether valid user orntot how to make that in same window after checking i have die(' not valid user ' ); i...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.