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

  • Home
  • SEARCH
  • 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 8144807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:28:50+00:00 2026-06-06T13:28:50+00:00

I have 3 fields and button: <input type=text id=a /> <input type=text id=b />

  • 0

I have 3 fields and button:

<input type="text" id="a" />
<input type="text" id="b" />
<input type="text" id="c" />
<input type="button" id="d" />
<label for="all_fields" class="error" style="display:none;">Please fill exactly one field</label>

I want to use jquery validator plugin in order to validate that exactly one field filled with text. What is the best way to do it?

  • 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-06T13:28:52+00:00Added an answer on June 6, 2026 at 1:28 pm

    Working demo for A / B / C case: http://jsfiddle.net/hfLwB/

    Behaviour: As long as one of the input field is set with nu,her value it will allow the calculation to happen, If all the 3 fields are empty it will thtourgh an error. You will also notice class .at_least_one and addMthod: require_from_group.

    Anyhoo code should speak better.

    Good link: http://docs.jquery.com/Plugins/Validation

    Hope it helps 🙂

    HTML

    <form action="results.whatever" target="_blank" id="HULK">
    
        <div style="clear:both">
        <label for="a">A</label>
        <div style="float:right">
                <input name="a" type="text" class="at_least_one idleField"></div>
        </div>
        <div style="clear:both">
            <label for="b">B</label>
            <div style="float:right">
                <input name="b" type="text" class="at_least_one idleField">
            </div>
        </div>
        <div style="clear:both">
            <label for="c">C</label>
            <div style="float:right">
                <input name="c" type="text" class="at_least_one idleField">
            </div>
        </div>
        <div id="button">
            <input name="" type="submit" value="Calculate" class="calc_button" style="cursor:hand">
        </div>
        </form>
    <div id="errorContainer">
        <h4>errors</h4>
        <ol>
        </ol>
    </div>
    

    ​
    Code

    jQuery.validator.addMethod("require_from_group", function(value, element, options) {
      var numberRequired = options[0];
      var selector = options[1];
      var fields = $(selector, element.form);
      var filled_fields = fields.filter(function() {
        // it's more clear to compare with empty string
        return $(this).val() != ""; 
      });
      var empty_fields = fields.not(filled_fields);
      // we will mark only first empty field as invalid
      if (filled_fields.length < numberRequired && empty_fields[0] == element) {
        return false;
      }
      return true;
    // {0} below is the 0th item in the options field
    }, jQuery.format("Please fill out at least {0} of these fields."));
    
    $(document).ready(function(){
      var container = $('#errorContainer');
      $("#HULK").validate(  {
    
        // We must to force validation of entire form because
        // when we remove value from 2-nd field 
        // the 1-st field becomes invalid.
        onfocusout: function() { $("#HULK").valid() },
        onkeyup: function() { $("#HULK").valid() },
    
        rules: {
          // note: you can use variable to avoid repeating 
          // of the same code
          a: { 
            number: true,
            require_from_group: [1, ".at_least_one"]
          },
          b: {
            number: true,
            require_from_group: [1,".at_least_one"]
          },
          c: {
            number: true,
            require_from_group: [1,".at_least_one"]
          }
        },
        success: function(label) {  
          label.html(" ").addClass("checked"); 
        },
        errorContainer: container,
        errorLabelContainer: $("ol", container),
        wrapper: 'li',
        meta: "validate"
      });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following button: <input type=button count='<?php echo count($_SESSION['children']);?>' name=children class=add_child value=Add Another
<form id=youtube-form method=post action=> <label for=searchField id=label-youtube-id>YouTube ID:</label> <input type=text id=searchField name=videoid class=search-field />
I have a form with two input fields, one text and one send button.
I have a form with input fields and a button which fades in when
I have one form with several text fields and one button. When i enter
Creating a JApplet I have 2 Text Fields, a button and a Text Area.
So I have the following html: <div id=divForComponents> <input type=button value=+ onclick=addFilter('divForComponents')/> </div> And
I have a Facelet file with JSF tags to display a label, an input
I have a form written below: <table> <tr> <td>Worker Type:</td> <td><input type=text id=WorkerTypeTB/></td> </tr>
I have a form which contains 4 text fields when the button is clicked

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.