 | 
September 5th, 2008, 06:29 PM
| | Familiar Sight | | Join Date: Oct 2007
Posts: 212
| | [Javascript] If checkbox C_1 is selected fields required?
Hi all.
I have this simple form.
I need if checkbox C_1 is selected:
1) field name="n_1" is required and accept only numbers;
2) field name="n_2" is required and accept only numbers;
I need if checkbox C_2 is selected:
3) field name="n_3" is required and accept only numbers;
4) field name="n_4" is required and accept only numbers;
If checkbox not selected no required fields.
Can you help me? -
-
<html>
-
<head>
-
-
<title>Popup</title>
-
-
<script language="javascript" type="text/javascript">
-
<!--
-
-
function Go(){
-
-
var f=document.getElementById('id_form')
-
var s='';
-
var re = new RegExp("^[0-9]+$");
-
-
for(var i=0;i<f.elements.length;i++){
-
-
if(f.elements[i].value == ""){
-
alert("KO!");
-
f.elements[i].focus();
-
return false;
-
-
}else if(f.elements[i].value.match(re)){
-
alert("Only numbers!");
-
f.elements[i].focus();
-
return false;
-
-
}else{
-
s+=f.elements[i].value+',';
-
}
-
}
-
alert("OK.");
-
-
}
-
-
//-->
-
</script>
-
-
</head>
-
-
<body>
-
-
<form id="id_form" name="myform">
-
-
<div align="center">
-
<table border="0" id="table1">
-
-
<td class=blub align="center"><input type="checkbox" name="C_1" value="ON"></td>
-
<td class=blub align="center"><input type="text" id="n_1" name="n_1" size="5"></td>
-
<td class=blub align="center"><input type="text" id="n_2" name="n_2" size="5"></td>
-
-
<td class=blub align="center"><input type="checkbox" name="C_2" value="ON"></td>
-
<td class=blub align="center"><input type="text" id="n_1" name="n_3" size="5"></td>
-
<td class=blub align="center"><input type="text" id="n_2" name="n_4" size="5"></td>
-
-
<td class=blub align="center"><a href="#" onclick="Go();">Go</a></td>
-
-
</tr>
-
-
</table>
-
</div>
-
-
</form>
-
-
</body>
-
</html>
| 
September 5th, 2008, 06:53 PM
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany Age: 31
Posts: 895
| |
you could (use two forms,) query the checkboxes with {checkbox_element}.checked and then select the forms/form elements to check for valid input.
regards
PS: you can omit the href attribute, if you style those anchors with css
| 
September 5th, 2008, 07:58 PM
| | Familiar Sight | | Join Date: Oct 2007
Posts: 212
| | Quote: |
Originally Posted by Dormilich you could (use two forms,) query the checkboxes with {checkbox_element}.checked and then select the forms/form elements to check for valid input.
regards
PS: you can omit the href attribute, if you style those anchors with css | mmm sorry but I dont understand....
| 
September 6th, 2008, 02:17 AM
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany Age: 31
Posts: 895
| | Quote: |
Originally Posted by viki1967 mmm sorry but I dont understand.... | what don't you understand? the checkbox thing? well every checkbox element has a property called 'checked'. you can test if this property is true or false and depending on that you can do the validation.
regards
| 
September 6th, 2008, 08:37 AM
| | Familiar Sight | | Join Date: Oct 2007
Posts: 212
| | Quote: |
Originally Posted by Dormilich what don't you understand? the checkbox thing? well every checkbox element has a property called 'checked'. you can test if this property is true or false and depending on that you can do the validation.
regards | No... I don't understand: "use two forms"....
one example please?
| 
September 6th, 2008, 10:26 AM
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany Age: 31
Posts: 895
| |
the two forms idea was only to point out that you need something to sort out which elements are checked with the first checkbox and which with the second. I've seen several possibilities
- group the elements by a class name (using later the getElementsByClassName() function)
- group the elements inside a wrapper element that is accessible via its id (this can be any, including form, div, span, table,...)
* if you use form as wrapper you have access through {form}.elements
* if you use generic elements you have access through {element}.childNodes
- assign an id to every input element and access them through a JS array that contains the appropriate ids
so, which one you choose depends on what you want to do with the form later and what you like best.
the two forms idea occurred to me because so it is possible to send only the required form.
regards
PS: as for the 'go' link
[HTML]// html
<span class="go" id="gocheck">Go!</span>
// css
.go {
text-decoration: underline;
color: _your_link_color_;
background-color: _your_link_background_color_;
}
.go:hover {
color: _your_link_color_on_hover_;
background-color: _your_link_background_color_on_hover_;
}
// javascript
var link = document.getElementById("gocheck");
link.addEventListener("click", Go, false); // standard
link.attachEvent("onclick", Go); // IE[/HTML]
| 
September 6th, 2008, 10:32 AM
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany Age: 31
Posts: 895
| |
One more thought. you could also use parseInt() or parseFloat() to check for a number.
regards
| 
September 6th, 2008, 12:29 PM
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 12,964
| |
If I'm not mistaken, the requirement is that the two fields n_1/n_2 are mandatory if the first checkbox is checked and the other two if the 2nd one is checked which doesn't mean that they shouldn't be sent if they are filled. In other words, I don't think two forms would be a good idea, but, as I said, I may be mistaken.
| 
September 6th, 2008, 01:51 PM
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany Age: 31
Posts: 895
| | Quote: |
Originally Posted by acoder If I'm not mistaken, the requirement is that the two fields n_1/n_2 are mandatory if the first checkbox is checked and the other two if the 2nd one is checked which doesn't mean that they shouldn't be sent if they are filled. In other words, I don't think two forms would be a good idea, but, as I said, I may be mistaken. | yea, it all depends on how the form data shall be sent, but it was not specified in the original post, so this idea popped up in my mind.
| 
September 6th, 2008, 10:47 PM
| | Familiar Sight | | Join Date: Oct 2007
Posts: 212
| |
sorry but I do not know this method....
| 
September 6th, 2008, 11:11 PM
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 12,964
| |
The basic idea is that when C1 is checked put the validation for the corresponding fields: - // C1 is checkbox ref.
-
if (C1.checked) {
-
// validation of fields...
-
}
| 
September 7th, 2008, 10:53 AM
| | Familiar Sight | | Join Date: Oct 2007
Posts: 212
| | Quote: |
Originally Posted by acoder The basic idea is that when C1 is checked put the validation for the corresponding fields: - // C1 is checkbox ref.
-
if (C1.checked) {
-
// validation of fields...
-
}
| Not working... no error but the form is validate until the checkbox no selected -
function Go(){
-
-
var f=document.getElementById('id_form')
-
var s="";
-
var re = new RegExp("^[0-9]+$");
-
-
for(var i=0;i<f.elements.length;i++){
-
-
if(f.elements[i].type=="checkbox" && f.elements[i].checked); {
-
-
if(f.elements[i].value == ""){
-
alert("KO!");
-
f.elements[i].focus();
-
return false;
-
}else
-
-
if(!f.elements[i].value.match(re)){
-
alert("Only numbers!");
-
f.elements[i].focus();
-
return false;
-
-
}else{
-
s+=f.elements[i].value+',';
-
}
-
}
-
}
-
-
alert("OK!");
-
-
-
}
-
-
| 
September 7th, 2008, 11:26 AM
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany Age: 31
Posts: 895
| |
This code looks as if only the selected checkbox is checked (and of cause the checkbox value is not a number). there's no correlation which elements are to be checked if any of the checkboxes is selected. (either you do a second for loop, or you use fixed values for the checkbox if-statement)
| 
September 7th, 2008, 11:38 AM
| | Familiar Sight | | Join Date: Oct 2007
Posts: 212
| | Quote: |
Originally Posted by Dormilich This code looks as if only the selected checkbox is checked (and of cause the checkbox value is not a number). there's no correlation which elements are to be checked if any of the checkboxes is selected. (either you do a second for loop, or you use fixed values for the checkbox if-statement) | I'am confused... -
-
-
function Go(){
-
-
var f=document.getElementById('id_form')
-
var s="";
-
var re = new RegExp("^[0-9]+$");
-
-
for(var i=0;i<f.elements.length;i++) {
-
-
if (f.elements[i].checked)
-
{
-
-
if(f.elements[i].value.length <= 0) {
-
alert("Non va bene!");
-
f.elements[i].focus();
-
return false;
-
}else
-
-
s+=f.elements[i].value+',';
-
}
-
}
-
-
alert("OK!");
-
-
-
}
-
-
| 
September 7th, 2008, 12:16 PM
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 12,964
| |
Don't loop over the elements array. Use fixed names/ids. That will allow you to get the correct elements. You could use the elements array/object if you provide names or set IDs and use document.getElementById().
| 
September 7th, 2008, 12:43 PM
| | Familiar Sight | | Join Date: Oct 2007
Posts: 212
| | Quote: |
Originally Posted by acoder Don't loop over the elements array. Use fixed names/ids. That will allow you to get the correct elements. You could use the elements array/object if you provide names or set IDs and use document.getElementById(). | I have change method and function javascript and semplify the code... but if checkbox not selected alert is always KO!
:( -
-
function submitIt()
-
-
{
-
-
-
-
var el = document.forms['myform'].elements;
-
var s = "";
-
-
-
var nFields = el.length;
-
-
for ( var n = 0 ; n < nFields ; n++ )
-
-
{
-
-
if (el[n].type == "checkbox" &&
-
el[n].checked); {
-
-
if(el[n].value.length <= 0)
-
-
{
-
-
alert("KO!");
-
el[n].focus();
-
return false;
-
-
}
-
-
else {
-
-
s+=el[n].value+',';
-
-
}
-
-
}
-
-
-
-
}
-
-
alert("OK!");
-
-
}
-
-
...
-
-
<form id="id_form" action="" name="myform">
-
-
...
-
-
<a href="#" onclick="return(submitIt(this));">GO!</a>
-
-
| 
September 7th, 2008, 01:22 PM
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 12,964
| |
The problem is still the same. After finding a checkbox and seeing that it's checked, you're checking the value of the same checkbox element (line 20). Also remove the semi-colon from line 18.
Based on the HTML you posted earlier, I suggest you try something like the following: - var C1 = f.elements["C_1"];
-
if (C1.checked) {
-
var n1 = document.getElementById("n_1");
-
// validate n1...
-
var n2 = document.getElementById("n_2");
-
// validate n2...
-
}
-
// now repeat for checkbox C_2...
No need for a for loop to loop over all the form elements.
| 
September 7th, 2008, 01:29 PM
| | Familiar Sight | | Join Date: Oct 2007
Posts: 212
| | Quote: |
Originally Posted by acoder The problem is still the same. After finding a checkbox and seeing that it's checked, you're checking the value of the same checkbox element (line 20). Also remove the semi-colon from line 18.
Based on the HTML you posted earlier, I suggest you try something like the following: - var C1 = f.elements["C_1"];
-
if (C1.checked) {
-
var n1 = document.getElementById("n_1");
-
// validate n1...
-
var n2 = document.getElementById("n_2");
-
// validate n2...
-
}
-
// now repeat for checkbox C_2...
No need for a for loop to loop over all the form elements. |
I need to loop because number of fields ( checkbox and other fields ) is variable in the form... do you understand my?
| 
September 7th, 2008, 01:30 PM
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 12,964
| |
Oh, I see. So then how would you determine which checkbox applies to which element?
| 
September 7th, 2008, 01:34 PM
| | Familiar Sight | | Join Date: Oct 2007
Posts: 212
| | Quote: |
Originally Posted by acoder Oh, I see. So then how would you determine which checkbox applies to which element? | this is the problem...
| 
September 7th, 2008, 03:04 PM
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 12,964
| |
Let's try again. Is it always one checkbox followed by two text boxes?
| 
September 7th, 2008, 03:53 PM
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany Age: 31
Posts: 895
| | Quote: |
Originally Posted by acoder Oh, I see. So then how would you determine which checkbox applies to which element? | this is why I thought about using several forms. Is it a good idea to use: (foreach) form.submit() to send all forms?
maybe you can add a class to every input group and access them through getElementsByClassName()...
regards
| 
September 7th, 2008, 04:00 PM
| | Familiar Sight | | Join Date: Oct 2007
Posts: 212
| | Quote: |
Originally Posted by acoder Let's try again. Is it always one checkbox followed by two text boxes? | Yes the form is always this: one checkbox followed by two text boxes.
| 
September 7th, 2008, 05:16 PM
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany Age: 31
Posts: 895
| | Quote: |
Originally Posted by viki1967 Yes the form is always this: one checkbox followed by two text boxes. | @ acoder: do you think node.nextSibling would be of use here?
| 
September 7th, 2008, 07:15 PM
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 12,964
| | Quote: |
Originally Posted by viki1967 Yes the form is always this: one checkbox followed by two text boxes. | Since this is the case and there are no submit buttons, you can loop over the form elements array and add 3 each time instead of incrementing one only, e.g. - for (n = 0; n < f.elements.length; n += 3) {
-
var chk = f.elements[n]; // checkbox
-
var txt1 = f.elements[n+1]; // text box 1
-
var txt2 = f.elements[n+2]; // text box 2
-
// now do the validation
-
| 
September 7th, 2008, 07:20 PM
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 12,964
| | Quote: |
Originally Posted by Dormilich this is why I thought about using several forms. Is it a good idea to use: (foreach) form.submit() to send all forms?
maybe you can add a class to every input group and access them through getElementsByClassName()... | That would be an idea, but rather than add it in the markup, I would say it would be better to check the type and check the next elements instead. I'm not too keen on the multiple forms idea because it causes more problems than it solves.
| 
September 7th, 2008, 07:21 PM
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 12,964
| | Quote: |
Originally Posted by Dormilich @ acoder: do you think node.nextSibling would be of use here? | Possibly or what I've posted above.
| 
September 7th, 2008, 08:55 PM
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany Age: 31
Posts: 895
| |
I'd go for your solution too (sounds more solid)
| 
September 8th, 2008, 04:38 PM
| | Familiar Sight | | Join Date: Oct 2007
Posts: 212
| |
sorry... but I have reset all... I am desperate because do not find solution to the my problem javascript...
| 
September 8th, 2008, 06:55 PM
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 12,964
| |
See #25 above. Did you try that?
| 
September 8th, 2008, 09:20 PM
| | Familiar Sight | | Join Date: Oct 2007
Posts: 212
| | Quote: |
Originally Posted by acoder See #25 above. Did you try that? | Yes but not working...
I write new function... try please this web page
This code working if one checkbox one input type text...
In my case I need: - checkbox - select - input text 1 - input text 2
and the string is: - name_checkbox , value_select , value_input_text_1 , value_input_text_2
can you help me?
| 
September 9th, 2008, 11:56 AM
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 12,964
| |
OK, so the requirements changed again. Why have you named the select object and text box elements with the same name. If you name them differently, it'll be easier to access them, e.g. C1sel for the select element, C1t1 for the first text box and C1t2 for the second text box.
| 
September 9th, 2008, 05:34 PM
| | Familiar Sight | | Join Date: Oct 2007
Posts: 212
| |
thanks acoder...I see the solution... try this please: http://free.7host06.com/popigu/
My problem this is: - else {
-
-
stringa+=checks[i].value+','+select.options[select.selectedIndex].value+","+input1.value+","+input2.value+",";
-
-
}
-
-
}
-
-
}
-
-
window.opener.document.myform.campo_form_pagina_madre.value = stringa
-
alert("OK.");
-
window.close();
-
-
return false;
-
}
| 
September 9th, 2008, 05:56 PM
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 12,964
| |
Good job! Is it all working fine now?
| 
September 9th, 2008, 06:01 PM
| | Familiar Sight | | Join Date: Oct 2007
Posts: 212
| | Quote: |
Originally Posted by acoder Good job! Is it all working fine now? |
No, working not fully...
the problem is this part of the code: - else {
-
-
stringa+=checks[i].value+','+select.options[select.selectedIndex].value+","+input1.value+","+input2.value+",";
-
-
}
-
-
}
-
-
}
-
-
window.opener.document.myform.campo_form_pagina_ma dre.value = stringa
-
alert("OK.");
-
window.close();
-
-
return false;
-
}
Because submit the form when only checkbox is selected... do you try my link? http://free.7host06.com/popigu/ | 
September 9th, 2008, 08:53 PM
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 12,964
| |
So you want it to submit/close even if no checkbox is checked?
| 
September 10th, 2008, 01:02 PM
| | Familiar Sight | | Join Date: Oct 2007
Posts: 212
| | Quote: |
Originally Posted by acoder So you want it to submit/close even if no checkbox is checked? | Hi acoder, thanks x your reply.
I close this project, the final and working solution is: -
-
<html>
-
<head>
-
-
<title>Popup</title>
-
-
<script type="text/javascript">
-
<!--//
-
-
function checkNumber(e)
-
{
-
var evt = window.event ? window.event : e;
-
var input = evt.target ? evt.target : evt.srcElement;
-
-
-
var value = input.value;
-
var chr = parseInt(value.substring(value.length - 1, value.length));
-
| | |