469,347 Members | 22,107 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,347 developers. It's quick & easy.

[Javascript] If checkbox C_1 is selected fields required?

263 100+
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?

Expand|Select|Wrap|Line Numbers
  1.  
  2. <html>
  3. <head>
  4.  
  5. <title>Popup</title>
  6.  
  7. <script language="javascript" type="text/javascript">
  8. <!--
  9.  
  10. function Go(){
  11.  
  12. var f=document.getElementById('id_form')
  13. var s='';
  14. var re = new RegExp("^[0-9]+$");
  15.  
  16. for(var i=0;i<f.elements.length;i++){
  17.  
  18. if(f.elements[i].value == ""){
  19. alert("KO!");
  20. f.elements[i].focus();
  21. return false;
  22.  
  23. }else if(f.elements[i].value.match(re)){
  24. alert("Only numbers!");
  25. f.elements[i].focus();
  26. return false;
  27.  
  28. }else{
  29.   s+=f.elements[i].value+',';
  30. }
  31.   alert("OK.");
  32.  
  33. }
  34.  
  35. //-->
  36. </script>
  37.  
  38. </head>
  39.  
  40. <body>
  41.  
  42. <form id="id_form" name="myform">
  43.  
  44. <div align="center">
  45. <table border="0" id="table1">
  46.  
  47. <td class=blub align="center"><input type="checkbox" name="C_1" value="ON"></td> 
  48. <td class=blub align="center"><input type="text" id="n_1" name="n_1" size="5"></td> 
  49. <td class=blub align="center"><input type="text" id="n_2" name="n_2" size="5"></td>
  50.  
  51. <td class=blub align="center"><input type="checkbox" name="C_2" value="ON"></td> 
  52. <td class=blub align="center"><input type="text" id="n_1" name="n_3" size="5"></td> 
  53. <td class=blub align="center"><input type="text" id="n_2" name="n_4" size="5"></td>
  54.  
  55. <td class=blub align="center"><a href="#" onclick="Go();">Go</a></td>
  56.  
  57. </tr>
  58.  
  59. </table>
  60.  </div>
  61.  
  62. </form>
  63.  
  64. </body>
  65. </html> 
Sep 5 '08 #1
37 4346
Dormilich
8,652 Expert Mod 8TB
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
Sep 5 '08 #2
viki1967
263 100+
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....
Sep 5 '08 #3
Dormilich
8,652 Expert Mod 8TB
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
Sep 6 '08 #4
viki1967
263 100+
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?
Sep 6 '08 #5
Dormilich
8,652 Expert Mod 8TB
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]
Sep 6 '08 #6
Dormilich
8,652 Expert Mod 8TB
One more thought. you could also use parseInt() or parseFloat() to check for a number.

regards
Sep 6 '08 #7
acoder
16,027 Expert Mod 8TB
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.
Sep 6 '08 #8
Dormilich
8,652 Expert Mod 8TB
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.
Sep 6 '08 #9
viki1967
263 100+
sorry but I do not know this method....
Sep 6 '08 #10
acoder
16,027 Expert Mod 8TB
The basic idea is that when C1 is checked put the validation for the corresponding fields:
Expand|Select|Wrap|Line Numbers
  1. // C1 is checkbox ref.
  2. if (C1.checked) {
  3. // validation of fields...
  4. }
Sep 6 '08 #11
viki1967
263 100+
The basic idea is that when C1 is checked put the validation for the corresponding fields:
Expand|Select|Wrap|Line Numbers
  1. // C1 is checkbox ref.
  2. if (C1.checked) {
  3. // validation of fields...
  4. }
Not working... no error but the form is validate until the checkbox no selected

Expand|Select|Wrap|Line Numbers
  1. function Go(){
  2.  
  3. var f=document.getElementById('id_form')
  4. var s="";
  5. var re = new RegExp("^[0-9]+$");
  6.  
  7. for(var i=0;i<f.elements.length;i++){
  8.  
  9. if(f.elements[i].type=="checkbox" && f.elements[i].checked); {
  10.  
  11. if(f.elements[i].value == ""){
  12. alert("KO!");
  13. f.elements[i].focus();
  14. return false;
  15. }else
  16.  
  17. if(!f.elements[i].value.match(re)){
  18. alert("Only numbers!");
  19. f.elements[i].focus();
  20. return false;
  21.  
  22. }else{
  23.   s+=f.elements[i].value+',';
  24. }
  25. }
  26. }
  27.  
  28.   alert("OK!");
  29.  
  30.  
  31. }
  32.  
  33.  
Sep 7 '08 #12
Dormilich
8,652 Expert Mod 8TB
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)
Sep 7 '08 #13
viki1967
263 100+
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...

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. function Go(){
  4.  
  5. var f=document.getElementById('id_form')
  6. var s="";
  7. var re = new RegExp("^[0-9]+$");
  8.  
  9. for(var i=0;i<f.elements.length;i++) {
  10.  
  11.   if (f.elements[i].checked)
  12.     {
  13.  
  14. if(f.elements[i].value.length <= 0) {
  15. alert("Non va bene!");
  16. f.elements[i].focus();
  17. return false;
  18. }else
  19.  
  20.   s+=f.elements[i].value+',';
  21. }
  22. }
  23.  
  24.   alert("OK!");
  25.  
  26.  
  27. }
  28.  
  29.  
Sep 7 '08 #14
acoder
16,027 Expert Mod 8TB
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().
Sep 7 '08 #15
viki1967
263 100+
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!

:(

Expand|Select|Wrap|Line Numbers
  1.  
  2. function submitIt()
  3.  
  4.  
  5.  
  6.  
  7.   var el = document.forms['myform'].elements;
  8.   var s = "";
  9.  
  10.  
  11.   var nFields = el.length;
  12.  
  13.   for ( var n = 0 ; n < nFields ; n++ )
  14.  
  15.     { 
  16.  
  17.       if (el[n].type == "checkbox" &&              
  18.           el[n].checked);  {                       
  19.  
  20.              if(el[n].value.length <= 0)           
  21.  
  22.                    {
  23.  
  24.                    alert("KO!");
  25.                    el[n].focus();
  26.                    return false;
  27.  
  28.                    }
  29.  
  30.       else {
  31.  
  32.         s+=el[n].value+',';
  33.  
  34.            }              
  35.  
  36. }
  37.  
  38.  
  39.  
  40. }
  41.  
  42.  alert("OK!");
  43.  
  44. }
  45.  
  46. ...
  47.  
  48. <form id="id_form" action="" name="myform">
  49.  
  50. ...
  51.  
  52. <a href="#" onclick="return(submitIt(this));">GO!</a>
  53.  
  54.  
Sep 7 '08 #16
acoder
16,027 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. var C1 = f.elements["C_1"];
  2. if (C1.checked) {
  3.     var n1 = document.getElementById("n_1");
  4.     // validate n1...
  5.     var n2 = document.getElementById("n_2");
  6.     // validate n2...
  7. }
  8. // now repeat for checkbox C_2...
No need for a for loop to loop over all the form elements.
Sep 7 '08 #17
viki1967
263 100+
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:
Expand|Select|Wrap|Line Numbers
  1. var C1 = f.elements["C_1"];
  2. if (C1.checked) {
  3.     var n1 = document.getElementById("n_1");
  4.     // validate n1...
  5.     var n2 = document.getElementById("n_2");
  6.     // validate n2...
  7. }
  8. // 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?
Sep 7 '08 #18
acoder
16,027 Expert Mod 8TB
Oh, I see. So then how would you determine which checkbox applies to which element?
Sep 7 '08 #19
viki1967
263 100+
Oh, I see. So then how would you determine which checkbox applies to which element?
this is the problem...
Sep 7 '08 #20
acoder
16,027 Expert Mod 8TB
Let's try again. Is it always one checkbox followed by two text boxes?
Sep 7 '08 #21
Dormilich
8,652 Expert Mod 8TB
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
Sep 7 '08 #22
viki1967
263 100+
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.
Sep 7 '08 #23
Dormilich
8,652 Expert Mod 8TB
Yes the form is always this: one checkbox followed by two text boxes.
@ acoder: do you think node.nextSibling would be of use here?
Sep 7 '08 #24
acoder
16,027 Expert Mod 8TB
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.
Expand|Select|Wrap|Line Numbers
  1. for (n = 0; n < f.elements.length; n += 3) {
  2.     var chk = f.elements[n]; // checkbox
  3.     var txt1 = f.elements[n+1]; // text box 1
  4.     var txt2 = f.elements[n+2]; // text box 2
  5.     // now do the validation
  6.  
Sep 7 '08 #25
acoder
16,027 Expert Mod 8TB
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.
Sep 7 '08 #26
acoder
16,027 Expert Mod 8TB
@ acoder: do you think node.nextSibling would be of use here?
Possibly or what I've posted above.
Sep 7 '08 #27
Dormilich
8,652 Expert Mod 8TB
I'd go for your solution too (sounds more solid)
Sep 7 '08 #28
viki1967
263 100+
sorry... but I have reset all... I am desperate because do not find solution to the my problem javascript...
Sep 8 '08 #29
acoder
16,027 Expert Mod 8TB
See #25 above. Did you try that?
Sep 8 '08 #30
viki1967
263 100+
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:

Expand|Select|Wrap|Line Numbers
  1. checkbox - select - input text 1 - input text 2 
and the string is:

Expand|Select|Wrap|Line Numbers
  1. name_checkbox , value_select , value_input_text_1 , value_input_text_2
can you help me?
Sep 8 '08 #31
acoder
16,027 Expert Mod 8TB
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.
Sep 9 '08 #32
viki1967
263 100+
thanks acoder...I see the solution... try this please:

http://free.7host06.com/popigu/

My problem this is:

Expand|Select|Wrap|Line Numbers
  1.       else {
  2.  
  3. stringa+=checks[i].value+','+select.options[select.selectedIndex].value+","+input1.value+","+input2.value+",";
  4.  
  5.  
  6. }
  7.  
  8.  
  9. window.opener.document.myform.campo_form_pagina_madre.value = stringa
  10. alert("OK.");
  11. window.close();
  12.  
  13. return false; 
  14. }
Sep 9 '08 #33
acoder
16,027 Expert Mod 8TB
Good job! Is it all working fine now?
Sep 9 '08 #34
viki1967
263 100+
Good job! Is it all working fine now?

No, working not fully...

the problem is this part of the code:

Expand|Select|Wrap|Line Numbers
  1. else {
  2.  
  3. stringa+=checks[i].value+','+select.options[select.selectedIndex].value+","+input1.value+","+input2.value+",";
  4.  
  5.  
  6. }
  7.  
  8.  
  9. window.opener.document.myform.campo_form_pagina_ma  dre.value = stringa
  10. alert("OK.");
  11. window.close();
  12.  
  13. return false; 
  14. }
Because submit the form when only checkbox is selected... do you try my link?

http://free.7host06.com/popigu/
Sep 9 '08 #35
acoder
16,027 Expert Mod 8TB
So you want it to submit/close even if no checkbox is checked?
Sep 9 '08 #36
viki1967
263 100+
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <html>
  3. <head>
  4.  
  5. <title>Popup</title>
  6.  
  7. <script type="text/javascript">
  8. <!--//
  9.  
  10. function checkNumber(e) 
  11.     var evt = window.event ? window.event : e; 
  12.     var input = evt.target ? evt.target : evt.srcElement; 
  13.  
  14.  
  15.         var value = input.value; 
  16.         var chr = parseInt(value.substring(value.length - 1, value.length)); 
  17.         if(!chr && chr != 0) 
  18.         { 
  19.             if(value.length == 1) 
  20.                 value = ''; 
  21.             else 
  22.                 value = value.substring(0, value.length - 1); 
  23.  
  24.             input.value = value; 
  25.         } 
  26.     } 
  27.  
  28. window.onkeyup = checkNumber;
  29.  
  30. /////////////////////////////
  31.  
  32. function checkForm() 
  33.    var checks = new Array(); 
  34.    var els = document.getElementsByTagName('input'); 
  35.  
  36.    var ret = true; 
  37.  
  38.    var num = 0; 
  39.  
  40.    var stringa='' 
  41.  
  42.    for(i = 0; i < els.length; i++) 
  43.    { 
  44.       if(els[i].type.toUpperCase() == "CHECKBOX") 
  45.          checks[num++] = els[i]; 
  46.    } 
  47.  
  48.    var countChecked = 0
  49.  
  50.    for(i = 0; i < checks.length; i++) 
  51.    { 
  52.       if(checks[i].checked) 
  53.       { 
  54.          countChecked++;
  55.  
  56.          var name = checks[i].name.substring(1); 
  57.          var select, input1, input2; 
  58.  
  59.          select = document.getElementById("Esito_"+name); 
  60.          input1 = document.getElementById("ITEMS_"+name); 
  61.          input2 = document.getElementById("ITEMS_KO_"+name+"_"+name); 
  62.  
  63.          if(select.selectedIndex <= 0 || input1.value == "" || input2.value == ""){ 
  64.  
  65.              alert("KO");
  66.              ret = false;  
  67.       } 
  68.  
  69.       else {
  70.  
  71.  
  72.         stringa+=checks[i].value+','+select.options[select.selectedIndex].value+","+input1.value+","+input2.value+",";
  73.  
  74.         window.opener.document.myform.campo_form_pagina_madre.value = stringa
  75.         window.close();
  76.         ret = false;  
  77.  
  78.            }
  79.       }
  80.  
  81.  
  82.  
  83. if(countChecked == 0)
  84. ret = false;
  85.  
  86. }
  87.  
  88. return ret; 
  89.  
  90.  
  91. }
  92.  
  93. /////////////////////////////
  94.  
  95. //-->
  96. </script> 
  97.  
  98.  
  99. </head>
  100.  
  101. <body>
  102.  
  103. <form id="id_form" name="myform">
  104.  
  105. <input type="checkbox" id="C1" name="C1" value="ON">
  106.  
  107. <select size="1" id="Esito_1" name="Esito_1">
  108.    <option>Seleziona</option>
  109.    <option value="Sufficiente">Sufficiente</option>
  110.    <option value="Insufficiente">Insufficiente</option>
  111.    <option value="Sufficiente con osservazioni">Sufficiente con osservazioni</option>
  112.    <option value="Dettagliato in verbale">Dettagliato in verbale</option>
  113. </select>
  114.  
  115. <input type="text" id="ITEMS_1" name="ITEMS_1" size="5" onKeyUp="checkNumber(event)" />
  116. <input type="text" id="ITEMS_KO_1_1" name="ITEMS_KO_1_1" size="5" onKeyUp="checkNumber(event)" />
  117.  
  118.  
  119. <br><br>
  120.  
  121. <input type="checkbox" id="SOTTODETTAGLIO_VERIFICA_2" name="SOTTODETTAGLIO_VERIFICA_2" value="ON">
  122.  
  123. <select size="1" id="Esito_2" name="Esito_2">
  124.    <option>Seleziona</option>
  125.    <option value="Sufficiente">Sufficiente</option>
  126.    <option value="Insufficiente">Insufficiente</option>
  127.    <option value="Sufficiente con osservazioni">Sufficiente con osservazioni</option>
  128.    <option value="Dettagliato in verbale">Dettagliato in verbale</option>
  129. </select>
  130.  
  131. <input type="text" id="ITEMS_2" name="ITEMS_2" size="5" onKeyUp="checkNumber(event)" />
  132. <input type="text" id="ITEMS_KO_2_2" name="ITEMS_KO_2_2" size="5" onKeyUp="checkNumber(event)" />
  133.  
  134. <br><br>
  135.  
  136. <input type="checkbox" id="SOTTODETTAGLIO_VERIFICA_3" name="SOTTODETTAGLIO_VERIFICA_3" value="ON">
  137.  
  138. <select size="1" id="Esito_3" name="Esito_3">
  139.    <option>Seleziona</option>
  140.    <option value="Sufficiente">Sufficiente</option>
  141.    <option value="Insufficiente">Insufficiente</option>
  142.    <option value="Sufficiente con osservazioni">Sufficiente con osservazioni</option>
  143.    <option value="Dettagliato in verbale">Dettagliato in verbale</option>
  144. </select>
  145.  
  146. <input type="text" id="ITEMS_3" name="ITEMS_3" size="5" onKeyUp="checkNumber(event)" />
  147. <input type="text" id="ITEMS_KO_3_3" name="ITEMS_KO_3_3" size="5" onKeyUp="checkNumber(event)" />
  148.  
  149. <br><br>  
  150.  
  151. <a href="#" onclick="return checkForm();">Go</a></td>
  152.  
  153.  
  154.  
  155. </form>
  156.  
  157. </body>
  158. </html>
Sep 10 '08 #37
acoder
16,027 Expert Mod 8TB
Glad you got it working and thanks for posting your solution :)
Sep 10 '08 #38

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Cliff R. | last post: by
1 post views Thread by kiran | last post: by
13 posts views Thread by Oleg Konovalov | last post: by
3 posts views Thread by anthonybrough | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.