473,385 Members | 1,829 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,385 software developers and data experts.

beginner: form validation checkbox problem

hello,

i am making a form using php/html/javascript
a part of the form is (email address) comming from a database.
the amount of addresses is always different.
every address has a checkbox.
people have the check at least one checkbox.

i have made a form check to see of at least one checkbox is selected.
this workts fine.

but the problem is:
when there is only one address the form validation does not work!

i cannot get it to work.

thanks,
---------------------------- source code form ---------------------
<form onsubmit='return FormZoekMail(this)' name='mail' action='<? echo
$_SERVER['PHP_SELF']; ?>' method='post'>
<input type='checkbox' name='mailadres[]' value='some value'>
---------------------------------------------------------------

----------------------- source code javascript
function ----------------------

function FormZoekMail(hetForm){
leeg = 0
aantalElementen = 0

aantalElementen = hetForm['mailadres[]'].length;

kort = hetForm['mailadres[]'];

for(x = 0; x < aantalElementen; x++){
if(kort[x].checked != true){
leeg++;
}
}


if(leeg == aantalElementen){
alert('u moet minstens een mailadres selecteren');
return(false);
}
-----------------------------------------------------------------
Jul 24 '06 #1
5 1901
nescio wrote:
hello,

i am making a form using php/html/javascript
a part of the form is (email address) comming from a database.
the amount of addresses is always different.
every address has a checkbox.
people have the check at least one checkbox.

i have made a form check to see of at least one checkbox is selected.
this workts fine.

but the problem is:
when there is only one address the form validation does not work!

i cannot get it to work.
Hi,

That is a mean one. :P
I remember pulling my hairs out over that.

It works like this:
Javascript will only make an array of your radiobuttons if it encouters more
than 1 element.
By design.
Just to make the life of developers misserable.
Fun, isn't it?
;-)

Solution: Just check if it is an array, if not, treat it like a simple
value.

Regards,
Erwin Moller
>
thanks,
---------------------------- source code form ---------------------
<form onsubmit='return FormZoekMail(this)' name='mail' action='<? echo
$_SERVER['PHP_SELF']; ?>' method='post'>
<input type='checkbox' name='mailadres[]' value='some value'>
---------------------------------------------------------------

----------------------- source code javascript
function ----------------------

function FormZoekMail(hetForm){
leeg = 0
aantalElementen = 0

aantalElementen = hetForm['mailadres[]'].length;

kort = hetForm['mailadres[]'];

for(x = 0; x < aantalElementen; x++){
if(kort[x].checked != true){
leeg++;
}
}


if(leeg == aantalElementen){
alert('u moet minstens een mailadres selecteren');
return(false);
}
-----------------------------------------------------------------
Jul 24 '06 #2
nescio wrote:
<snip>
but the problem is:
when there is only one address the form validation does
not work!

i cannot get it to work.
<snip>

<URL: http://jibbering.com/faq/faq_notes/f...ess.html#faBut >

Richard.
Jul 24 '06 #3
Solution: Just check if it is an array, if not, treat it like a simple
value.
thanks for your advice.

first of all, my knowledge of javascript is rather limited, but i am eager
to learn.

it tried the function typeof() to find out if there is one or more then one
element(s).
but in both cases it returns 'object';

in the form there are brackets around the name field:
<input type = checkbox name='emailadres[]' value = 'something'>

these brackets can't be omitted because php needs them.

so, my next question is: how do i find, using javascript, if the value is an
array or a string or whatever.
thanks
Jul 25 '06 #4
nescio wrote:
>Solution: Just check if it is an array, if not, treat it like a simple
value.

thanks for your advice.

first of all, my knowledge of javascript is rather limited, but i am eager
to learn.

it tried the function typeof()
It is not a function; - typeof - is an operator. I discourage the habit
of putting parenthesise around its operand because that does act to
confuse people into thinking that it is a function and that its operand
is an argument to a function call. What is happening with - typeof(x) -
is that the tokenizer sees that the first - ( - cannot be a character
in an identifier so it can see that - typeof - is a single token, which
it then sees as a keyword token declaring the - typeof - operator. It
is a unary operator which expects a right hand side operand that is an
expression, so the interpreter tries to find an expression if the
following tokens, and finds one in the form of - (x) -, which is the
grouping operators (the parentheses) surrounding their operand (the
Identifier - x - in this case).

The result may resemble a function call but - typeof(x) - is
functionally equivalent to - typeof x - (and one character longer).

to find out if there is one or more then one
element(s).
but in both cases it returns 'object';
The possibilities are a single <INPUT type="checkbox"element or a
collection of such elements, both are objects so will be reported as
objects.
in the form there are brackets around the name field:
<input type = checkbox name='emailadres[]' value = 'something'>

these brackets can't be omitted because php needs them.
That is not strictly true, but not important as you can use bracket
notation in javascript to avoid the square brackets in the name being
an issue:-

<URL: http://www.jibbering.com/faq/faq_not..._brackets.html >
so, my next question is: how do i find, using javascript, if the value
is an array or a string or whatever.
It will never be a string, and a collection is only array-like, it is
not actually and Array (in the javascript sense). See:-

<URL: http://www.jibbering.com/faq/faq_not...ess.html#faBut >

Richard.

Jul 25 '06 #5
it is all working now, thank you all for your help
Jul 26 '06 #6

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

Similar topics

17
by: Phil Powell | last post by:
Where can I find an online PHP form validator script library to use? I have tried hacking the one here at work for weeks now and it's getting more and more impossible to customize, especially now...
21
by: AnnMarie | last post by:
<script language="JavaScript" type="text/javascript"> <!-- function validate(theForm) { var validity = true; // assume valid if(frmComments.name.value=='' && validity == true) { alert('Your...
5
by: KJ | last post by:
I need help. I have a checkbox and two textboxes on a webform. How can I validation if a person either enters something in the two textboxes OR the checkbox? I tried using a custom validator...
9
by: julie.siebel | last post by:
Hello all! As embarrassing as it is to admit this, I've been designing db driven websites using javascript and vbscript for about 6-7 years now, and I am *horrible* at form validation. To be...
4
by: timothy.pollard | last post by:
Hi all A few weeks ago a nice man called Evertjan helped me create a form validation system that took a table of four columns of checkboxes and: - allowed only one checkbox in each row to be...
12
by: tadisaus2 | last post by:
Hello, Checkbox form validation - how to make a user select 4 check boxes? I have a question of a few checkboxes and how do I require a user to check 2 checkboxes (no more, no less)? Here is my...
0
by: Ned Balzer | last post by:
I posted this this morning but it never went through, so I am trying again -- apologies for the duplication if so. I need to validate several checkboxes on an asp.net 2.0 page. I don't need to...
13
by: Andrew Falanga | last post by:
HI, Just a warning, I'm a javascript neophyte. I'm writing a function to validate the contents of a form on a web page I'm developing. Since I'm a neophyte, this function is quite simple at...
3
by: arty | last post by:
So basically i have a form with a action attribute set to another form, it is a survey spread among several pages and using php sessions, now im trying to validate the form with Javascript but it...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.