Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old September 5th, 2008, 06:29 PM
Familiar Sight
 
Join Date: Oct 2007
Posts: 212
Default [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?

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> 
Reply
  #2  
Old September 5th, 2008, 06:53 PM
Dormilich's Avatar
Expert
 
Join Date: Aug 2008
Location: Leipzig, Germany
Age: 31
Posts: 636
Default

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
Reply
  #3  
Old September 5th, 2008, 07:58 PM
Familiar Sight
 
Join Date: Oct 2007
Posts: 212
Default

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....
Reply
  #4  
Old September 6th, 2008, 02:17 AM
Dormilich's Avatar
Expert
 
Join Date: Aug 2008
Location: Leipzig, Germany
Age: 31
Posts: 636
Default

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
Reply
  #5  
Old September 6th, 2008, 08:37 AM
Familiar Sight
 
Join Date: Oct 2007
Posts: 212
Default

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?
Reply
  #6  
Old September 6th, 2008, 10:26 AM
Dormilich's Avatar
Expert
 
Join Date: Aug 2008
Location: Leipzig, Germany
Age: 31
Posts: 636
Default

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]
Reply
  #7  
Old September 6th, 2008, 10:32 AM
Dormilich's Avatar
Expert
 
Join Date: Aug 2008
Location: Leipzig, Germany
Age: 31
Posts: 636
Default

One more thought. you could also use parseInt() or parseFloat() to check for a number.

regards
Reply
  #8  
Old September 6th, 2008, 12:29 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,727
Default

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.
Reply
  #9  
Old September 6th, 2008, 01:51 PM
Dormilich's Avatar
Expert
 
Join Date: Aug 2008
Location: Leipzig, Germany
Age: 31
Posts: 636
Default

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.
Reply
  #10  
Old September 6th, 2008, 10:47 PM
Familiar Sight
 
Join Date: Oct 2007
Posts: 212
Default

sorry but I do not know this method....
Reply
  #11  
Old September 6th, 2008, 11:11 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,727
Default

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. }
Reply
  #12  
Old September 7th, 2008, 10:53 AM
Familiar Sight
 
Join Date: Oct 2007
Posts: 212
Default

Quote:
Originally Posted by acoder
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.  
Reply
  #13  
Old September 7th, 2008, 11:26 AM
Dormilich's Avatar
Expert
 
Join Date: Aug 2008
Location: Leipzig, Germany
Age: 31
Posts: 636
Default

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)
Reply
  #14  
Old September 7th, 2008, 11:38 AM
Familiar Sight
 
Join Date: Oct 2007
Posts: 212
Default

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...

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.  
Reply
  #15  
Old September 7th, 2008, 12:16 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,727
Default

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().
Reply
  #16  
Old September 7th, 2008, 12:43 PM
Familiar Sight
 
Join Date: Oct 2007
Posts: 212
Default

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!

:(

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.  
Reply
  #17  
Old September 7th, 2008, 01:22 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,727
Default

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.
Reply
  #18  
Old September 7th, 2008, 01:29 PM
Familiar Sight
 
Join Date: Oct 2007
Posts: 212
Default

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:
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?
Reply
  #19  
Old September 7th, 2008, 01:30 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,727
Default

Oh, I see. So then how would you determine which checkbox applies to which element?
Reply
  #20  
Old September 7th, 2008, 01:34 PM
Familiar Sight
 
Join Date: Oct 2007
Posts: 212
Default

Quote:
Originally Posted by acoder
Oh, I see. So then how would you determine which checkbox applies to which element?
this is the problem...
Reply
  #21  
Old September 7th, 2008, 03:04 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,727
Default

Let's try again. Is it always one checkbox followed by two text boxes?
Reply
  #22  
Old September 7th, 2008, 03:53 PM
Dormilich's Avatar
Expert
 
Join Date: Aug 2008
Location: Leipzig, Germany
Age: 31
Posts: 636
Default

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
Reply
  #23  
Old September 7th, 2008, 04:00 PM
Familiar Sight
 
Join Date: Oct 2007
Posts: 212
Default

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.
Reply
  #24  
Old September 7th, 2008, 05:16 PM
Dormilich's Avatar
Expert
 
Join Date: Aug 2008
Location: Leipzig, Germany
Age: 31
Posts: 636
Default

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?
Reply
  #25  
Old September 7th, 2008, 07:15 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,727
Default

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.
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.  
Reply
  #26  
Old September 7th, 2008, 07:20 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,727
Default

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.
Reply
  #27  
Old September 7th, 2008, 07:21 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,727
Default

Quote:
Originally Posted by Dormilich
@ acoder: do you think node.nextSibling would be of use here?
Possibly or what I've posted above.
Reply
  #28  
Old September 7th, 2008, 08:55 PM
Dormilich's Avatar
Expert
 
Join Date: Aug 2008
Location: Leipzig, Germany
Age: 31
Posts: 636
Default

I'd go for your solution too (sounds more solid)
Reply
  #29  
Old September 8th, 2008, 04:38 PM
Familiar Sight
 
Join Date: Oct 2007
Posts: 212
Default

sorry... but I have reset all... I am desperate because do not find solution to the my problem javascript...
Reply
  #30  
Old September 8th, 2008, 06:55 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,727
Default

See #25 above. Did you try that?
Reply
  #31  
Old September 8th, 2008, 09:20 PM
Familiar Sight
 
Join Date: Oct 2007
Posts: 212
Default

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:

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?
Reply
  #32  
Old September 9th, 2008, 11:56 AM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,727
Default

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.
Reply
  #33  
Old September 9th, 2008, 05:34 PM
Familiar Sight
 
Join Date: Oct 2007
Posts: 212
Default

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. }
Reply
  #34  
Old September 9th, 2008, 05:56 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,727
Default

Good job! Is it all working fine now?
Reply
  #35  
Old September 9th, 2008, 06:01 PM
Familiar Sight
 
Join Date: Oct 2007
Posts: 212
Default

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:

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/
Reply
  #36  
Old September 9th, 2008, 08:53 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,727
Default

So you want it to submit/close even if no checkbox is checked?
Reply
  #37  
Old September 10th, 2008, 01:02 PM
Familiar Sight
 
Join Date: Oct 2007
Posts: 212
Default

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:

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