Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9028069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:53:52+00:00 2026-06-16T06:53:52+00:00

This question is an extension of my SO question: jQuery to disable/enable textarea depending

  • 0

This question is an extension of my SO question:

jQuery to disable/enable textarea depending on which radio button is selected inside of loop

I’m using the jQuery bassistance validation plugin (http://bassistance.de/jquery-plugins/jquery-plugin-validation/).

The validation rule I’m trying to create is if a radio button equals ‘yes’, then make the textarea a required field. My ID’s and form field names for the radio and textarea boxes are dynamic but begin with the same word so I can use name^='travel'

HTML
UPDATED

<div class="controls">
    <label class="radio inline">
        <input type="radio" name="travel_1" data-travelNumber="1" id="travel_yes_1" value="Yes" tabindex="25" >
        Yes </label>
    <label class="radio inline">
        <input name="travel_1" type="radio" data-travelNumber="1" id="travel_no_1" value="No" tabindex="26" checked >
        No </label>
</div>
<div class="controls" id="Answer_travel_yes_1">
    <textarea name="travelDetails_1" id="travelDetails_1" ></textarea>
</div>

<div class="controls">
    <label class="radio inline">
        <input type="radio" name="travel_2" data-travelNumber="2" id="travel_yes_2" value="Yes" tabindex="25" >
        Yes </label>
    <label class="radio inline">
        <input name="travel_2" type="radio" data-travelNumber="2" id="travel_no_2" value="No" tabindex="26" checked >
        No </label>
</div>
<div class="controls" id="Answer_travel_yes_2">
    <textarea name="travelDetails_2" id="travelDetails_2"></textarea>
</div>


<div class="controls">
    <label class="radio inline">
        <input type="radio" name="travel_3" data-travelNumber="3" id="travel_yes_3" value="Yes" tabindex="25" >
        Yes </label>
    <label class="radio inline">
        <input name="travel_3" type="radio" data-travelNumber="3" id="travel_no_3" value="No" tabindex="26" checked >
        No </label>
</div>
<div class="controls" id="Answer_travel_yes_3">
    <textarea name="travelDetails_3" id="travelDetails_3"></textarea>
</div>

jQuery

//  I use this to hide/show the travelDetails textarea (show on yes, hide on no)
$('input:radio[name^="travel"]:checked').live('click',function(){
    var id = $(this).attr("id").split("_");
    id = id[id.length-1];
    $("#travelDetails_" + id).attr("disabled", !($(this).val() == "Yes"));
    if ( $(this).val() == 'Yes' ) 
        $('#Answer_travel_yes_' + id).slideDown();
    else
        $('#Answer_travel_yes_' + id).slideUp();
});


$("#TravelForm").validate({
        rules:{
            whatgoeshere:{
                    required: function(){
                        if ( $("input:radio[name^=travel]:checked").val() == "Yes" ){
                            //  make travel
                            true
                        }
                        else{
                            console.log('false');
                            true
                        }
          },
                    minlength: 10   // needs to have at least 10 characters entered (if the corresponding radio is Yes
            }
        },
        messages:{
            whatgoeshere:{
                required:"Please enter your travel details",
                minlength:"Please enter at least 10 characters"
            }
        },
        errorClass: "help-inline",
        errorElement: "span",
        highlight:function(element, errorClass, validClass) {
            $(element).parents('.control-group').addClass('error');
        },
        unhighlight: function(element, errorClass, validClass) {
            $(element).parents('.control-group').removeClass('error');
            //  $(element).parents('.control-group').addClass('success');
        }
    });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T06:53:53+00:00Added an answer on June 16, 2026 at 6:53 am

    This is a generic answer which you can apply into your code…


    I am fairly certain you cannot use a function inside the rules option. As per documentation, rules: need to be “key/value pairs”, unless using the depends property.

    rules: {
        mytextarea: {
            minlength: 10,
            required: {
                depends: function(element) {
                    return $("#myradio:checked")
                }
            }
        }
    },
    messages: {
        mytextarea: {
            required: "Please enter your travel details",
            minlength: "Please enter at least 10 characters"
        }
    }
    

    mytextarea would be the value of the name attribute of your textarea.

    <textarea name="mytextarea"></textarea>
    

    Alternatively…

    Apply jQuery .validate() rules dynamically.

    First initialize your .validate() plugin with options:

    $('form').validate({
        // your options, leave out the rule in question
    });
    

    Create a rules array (note the slightly different format when using messages):

    var myrule = {
        required: true,
        minlength: 10,
        messages: {
            required: "Please enter your travel details",
            minlength: "Please enter at least 10 characters"
        }
    }
    

    Then within your conditional, target the textarea as required:

    if ($(this).val() == 'Yes') {
        $('[name^="travel"]').rules('add', myrule);
    } else {
        $('[name^="travel"]').rules('remove', myrule);
    }
    

    http://docs.jquery.com/Plugins/Validation/rules#.22add.22rules


    BTW, .live() has been deprecated in favor of .on().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is an extension of jQuery Character Counter using class? . I would
This question is an extension to the one raised here: Using factory_girl in Rails
This is an extension to a previous question which was answered. But have a
I'm creating a chrome extension which uses http://loopj.com/jquery-tokeninput/ to add tokens, see previous question
This question is extension to the question here . I am using the code
this question is an extension of the question here i tried to extend Dane's
This question is an extension of this Related question . Taking Derick's advice, I
This question already has an answer here: What are Extension Methods? 5 answers Usage
As an extension to this question here Linking JavaScript Libraries in User Controls I
This is an extension for this question asked an hour ago. We cannot modify

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.