83547

textbox must not be empty if checkbox is ticked

Question:

I have three checkboxes. Each checkbox has a 'name' and 'age' text box next to them. 'Singles' checkbox box has one set of these textboxes while 'Couples' checkbox has two sets. I'm looking for code which will provide an 'alert' message prior to submitting a form to say all textboxes must be filled in if corresponding checkbox is ticked. For instance, if only 'Singles' checkbox is ticked, only 'Singles' Textboxes to be checked. If 'Singles' and 'Couples' Checked, Textboxes for 'Singles' and 'Couples' must be complete. I've tried the below code:

Please note, there is also a check to make sure a 'yournameis' is entered at the start and that indeed a checkbox is ticked. Thanks.

function validate_checkboxform( form ) { //Custom Validation Steps if( ltrim(rtrim(form.elements['yournameis'].value,' '),' ')=="" ) { alert("Please enter text. "); form.elements['yournameis'].focus(); return false; } //Custom Validation for element var fo = document.checkboxform; var checkboxform=document.getElementById("checkboxform"); if (!fo.singles1.checked && !fo.couples1.checked && !fo.families1.checked) { alert("You must select at least one option."); return false; } if (this.singles1.checked && this.singlesname1.value=="") { alert("Please enter something in input 1"); this.singlesname1.focus(); return false; } if (this.couples1.checked && this.couplesname1.value=="") { alert("Please enter something in input 2"); this.couplesname1.focus(); return false; } return true; }

Answer1:

function validate_checkboxform( form ) { if($('#isCheckBox').is(':checked') && $("#textBox").val().length <0) alert("value of textBox needed.."); } <input type="checkbox" id="isCheckBox"/> <input type="text" id="textBox"/>

Repeat the same for 3 checkboxes & inputs <a href="http://jsfiddle.net/q8zq83ud/1/" rel="nofollow">fiddle 'd here</a>

Recommend

  • Collision Detection using bounded boxes
  • integrating python scripts with django
  • Insert a TextBox and set the formula
  • D3: How to draw multiple Convex hulls on Groups of Force layout nodes?
  • How can I change the ForeColor of the GroupBox text without changing its children ForeColor?
  • Prevent timepicker addon from opening calendar
  • Get enum value from string
  • Get both date and time in milliseconds
  • Set Initial Value for Required attribute
  • Using self.id to populate other fields in Django
  • draw pie chart using iOS quartz 2D
  • How to expand parent div to account for child's overflow?
  • Why cout is producing no output on Code Blocks?
  • jquery code not working without breakpoint
  • Defining enums in Cython code that will be used in the C part of code
  • How to display special characters in SQL server 2008?
  • Magento get URL before current
  • Error in installing package: fatal error: stdlib.h: no such file or directory
  • Prevent Tomcat from caching request during starup
  • How to make R's read_csv2() recognise the text characters properly
  • How do I signal completion of my dataflow?
  • How to use JavaScript to determine whether a file exists in a directory?
  • How to avoid particles glitching together in an elastic particle collision simulator?
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • NHibernate Validation Localization with S#arp Architecture
  • Highlight one bar in a series in highcharts?
  • Recording logins for password protected directories
  • Splitting given String into two variables - php
  • Deselecting radio buttons while keeping the View Model in synch
  • Nant, Vault & Windows Integrated Authentication
  • Bug in WPF DataGrid
  • Check if a string to interpolate provides expected placeholders
  • Validaiting emails with Net.Mail MailAddress
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Javascript + PHP Encryption with pidCrypt
  • Redux, normalised entities and lodash merge
  • Calling of Constructors in a Java
  • PHP: When would you need the self:: keyword?
  • how does django model after text[] in postgresql [duplicate]
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)