[Javascript] If checkbox C_1 is selected fields required? | Familiar Sight | | Join Date: Oct 2007
Posts: 254
| |
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>
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,648
| | | re: [Javascript] If checkbox C_1 is selected fields required?
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
| | Familiar Sight | | Join Date: Oct 2007
Posts: 254
| | | re: [Javascript] If checkbox C_1 is selected fields required? 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....
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,648
| | | re: [Javascript] If checkbox C_1 is selected fields required? 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
| | Familiar Sight | | Join Date: Oct 2007
Posts: 254
| | | re: [Javascript] If checkbox C_1 is selected fields required? 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?
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,648
| | | re: [Javascript] If checkbox C_1 is selected fields required?
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]
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,648
| | | re: [Javascript] If checkbox C_1 is selected fields required?
One more thought. you could also use parseInt() or parseFloat() to check for a number.
regards
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: [Javascript] If checkbox C_1 is selected fields required?
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.
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,648
| | | re: [Javascript] If checkbox C_1 is selected fields required? 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.
| | Familiar Sight | | Join Date: Oct 2007
Posts: 254
| | | re: [Javascript] If checkbox C_1 is selected fields required?
sorry but I do not know this method....
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: [Javascript] If checkbox C_1 is selected fields required?
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...
-
}
| | Familiar Sight | | Join Date: Oct 2007
Posts: 254
| | | re: [Javascript] If checkbox C_1 is selected fields required? 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!");
-
-
-
}
-
-
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,648
| | | re: [Javascript] If checkbox C_1 is selected fields required?
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)
| | Familiar Sight | | Join Date: Oct 2007
Posts: 254
| | | re: [Javascript] If checkbox C_1 is selected fields required? 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!");
-
-
-
}
-
-
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: [Javascript] If checkbox C_1 is selected fields required?
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().
| | Familiar Sight | | Join Date: Oct 2007
Posts: 254
| | | re: [Javascript] If checkbox C_1 is selected fields required? 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>
-
-
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: [Javascript] If checkbox C_1 is selected fields required?
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.
| | Familiar Sight | | Join Date: Oct 2007
Posts: 254
| | | re: [Javascript] If checkbox C_1 is selected fields required? 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?
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: [Javascript] If checkbox C_1 is selected fields required?
Oh, I see. So then how would you determine which checkbox applies to which element?
| | Familiar Sight | | Join Date: Oct 2007
Posts: 254
| | | re: [Javascript] If checkbox C_1 is selected fields required? Quote:
Originally Posted by acoder Oh, I see. So then how would you determine which checkbox applies to which element? this is the problem...
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: [Javascript] If checkbox C_1 is selected fields required?
Let's try again. Is it always one checkbox followed by two text boxes?
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,648
| | | re: [Javascript] If checkbox C_1 is selected fields required? 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
| | Familiar Sight | | Join Date: Oct 2007
Posts: 254
| | | re: [Javascript] If checkbox C_1 is selected fields required? 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.
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,648
| | | re: [Javascript] If checkbox C_1 is selected fields required? 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?
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: [Javascript] If checkbox C_1 is selected fields required? 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
-
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: [Javascript] If checkbox C_1 is selected fields required? 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.
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: [Javascript] If checkbox C_1 is selected fields required? Quote:
Originally Posted by Dormilich @ acoder: do you think node.nextSibling would be of use here? Possibly or what I've posted above.
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,648
| | | re: [Javascript] If checkbox C_1 is selected fields required?
I'd go for your solution too (sounds more solid)
| | Familiar Sight | | Join Date: Oct 2007
Posts: 254
| | | re: [Javascript] If checkbox C_1 is selected fields required?
sorry... but I have reset all... I am desperate because do not find solution to the my problem javascript...
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: [Javascript] If checkbox C_1 is selected fields required?
See #25 above. Did you try that?
| | Familiar Sight | | Join Date: Oct 2007
Posts: 254
| | | re: [Javascript] If checkbox C_1 is selected fields required? 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?
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: [Javascript] If checkbox C_1 is selected fields required?
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.
| | Familiar Sight | | Join Date: Oct 2007
Posts: 254
| | | re: [Javascript] If checkbox C_1 is selected fields required?
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;
-
}
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: [Javascript] If checkbox C_1 is selected fields required?
Good job! Is it all working fine now?
| | Familiar Sight | | Join Date: Oct 2007
Posts: 254
| | | re: [Javascript] If checkbox C_1 is selected fields required? 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/ |  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: [Javascript] If checkbox C_1 is selected fields required?
So you want it to submit/close even if no checkbox is checked?
| | Familiar Sight | | Join Date: Oct 2007
Posts: 254
| | | re: [Javascript] If checkbox C_1 is selected fields required? 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: |  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: [Javascript] If checkbox C_1 is selected fields required?
Glad you got it working and thanks for posting your solution :)
|  | Similar JavaScript / Ajax / DHTML bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,419 network members.
|