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 9226581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:51:57+00:00 2026-06-18T04:51:57+00:00

If the checkbox is unchecked and the textbox is empty then I need the

  • 0

If the checkbox is unchecked and the textbox is empty then I need the validation message to pop up. It does pop up but deletes the text as it is entered keeping it as invalid. How do I make it so the validation disappears when the first character is typed and doesn’t reappear unless the textbox is blank?

<form  id="practiceForm">
    <input type="text" name="firstName" id="textbox"/>
    <input type="checkbox" name="active" id="checkbox"/>
    <input type="submit" id="submit" value="Submit"/>
</form>

<script type="text/javascript">
    $('#checkbox').attr('checked', true);

    if($('#checkbox').attr('checked')){
        $('#textbox').attr('disabled', true);
        $('#textbox').val('');
    } else {
        $('#textbox').attr('disabled', false);
    };


$('#checkbox').change(function () {
    if($('#checkbox').attr('checked')){
        $('#textbox').attr('disabled', true);
        $('#textbox').val('');
    } else {
        $('#textbox').attr('disabled', false);
    };
});

$.validator.addMethod("textValidate", function(value){
    if(!$('#checkbox').attr('checked')){
        if(!$('#textbox').val('') || !$('#textbox').val(null)){

            return true;
        };
    };  
}, "If box is not checked then there must be text"
);


$('#practiceForm').validate({
    rules: {
        //textValidate: true
        firstName:{ 
                    textValidate: true
                    }
                }
            });

</script>
  • 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-18T04:51:58+00:00Added an answer on June 18, 2026 at 4:51 am

    This logic inside your textValidate method is broken:

    if(!$('#textbox').val('') || !$('#textbox').val(null)){
    

    Instead of checking for a value of '' or null, you were setting that as the value. Since the method is called on every key-up event, it was wiping out the input as you typed.

    Try this instead:

    $.validator.addMethod("textValidate", function (value) {
        if (!$('#checkbox').attr('checked')) {
            if (!$('#textbox').val() == '' || !$('#textbox').val() == null) {
                 return true;
            };
        };
    }, "If box is not checked then there must be text");
    

    Working Demo: http://jsfiddle.net/s2AjA/

    Side issue:

    You don’t need most of this code…

    $('#checkbox').attr('checked', true);
    
    if($('#checkbox').attr('checked')){
        $('#textbox').attr('disabled', true);
        $('#textbox').val('');
    } else {
        $('#textbox').attr('disabled', false);
    };
    

    It’s only run once on DOM ready and since you set #checkbox as checked, the if/then conditional that looks as the checked property is totally superfluous & unnecessary.

    It can be more simply written as the following. Also changed out attr with prop which is technically more correct than attr when using jQuery 1.6+.

    $('#checkbox').prop('checked', true);
    $('#textbox').prop('disabled', true);
    $('#textbox').val('');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In jQuery, how does one go about finding all the 'unchecked' checked boxes. $(':checkbox:checked');
I've a simple situation. When the checkbox is checked then the textbox is shown.
So I am adding text and textbox to a page dynamically if a checkbox
I want to copy the whole rows ,but checkbox unchecked when clicking add button.(in
When I click on an unchecked checkbox, it gets checked and a dialog appears
How do i set the checkbox as unchecked in the HTML? I tried these
I want to check if a checkbox just got unchecked, when a user clicks
I want to run a function when a checkbox is checked or unchecked. However,
I'm trying to wire up a CheckBox to handle an event when check/unchecked. If
I tried using this <input type=text name=commentID id=commentID onkeyup=userTyped('skipID', this)/> <input type=checkbox name=skipID value=N

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.