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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:48:57+00:00 2026-06-17T16:48:57+00:00

Where do I put the logic for the JQuery Validation plug-in to validate fields

  • 0

Where do I put the logic for the JQuery Validation plug-in to validate fields based on which one is visible?

<%= form_for @contact, html: { class: "form-horizontal" } do |f| %>

  <div class="control-group">
    <%= f.label :name, :class => 'control-label' %>
    <div class="controls">
      <%= f.text_field :name, class: 'required' %>
    </div>
  </div>

  <div class="control-group">
    <%= f.label "How would you like to be contacted?", :class => 'control-label' %>
    <div class="controls">
      <div class="btn-group" data-toggle="buttons-radio">
        <button class="btn" type="button" name="contact_type" id="email_button" value="email">
          Email
        </button>
        <button class="btn" type="button" name="contact_type" id="telephone_button" value="phone">
          Telephone
        </button>
      </div>
    </div>
  </div>

  <div class="control-group" id="email">
    <%= f.label :email, :class => 'control-label' %>
    <div class="controls">
      <%= f.text_field :email %>
    </div>
  </div>

  <div class="control-group" id="telephone">
    <%= f.label :telephone, :class => 'control-label' %>
    <div class="controls">
      <%= f.text_field :telephone %>
    </div>
  </div>

  <div class="control-group">
    <div class="controls">
      <%= f.submit "Submit", class: "btn btn-primary" %>
    </div>
  </div>
<% end %>

JavaScript

$(document).ready(function() {
  $('#email').hide();
  $('#telephone').hide();

  var email = false;
  var telephone = false;

  $('#email_button').click(function() {
    $('#email').show();
    $('#telephone').hide();

    email = true;
    telephone = false;
  });
  $('#telephone_button').click(function() {
    $('#telephone').show();
    $('#email').hide();

    telephone = true;
    email = false;
  });

  jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
    phone_number = phone_number.replace(/\s+/g, "");
    return this.optional(element) ||
        phone_number.match(/^[2-9]\d{2}-[2-9]\d{2}-\d{4}$/);
  }, "Please specify a valid phone number");

  $('#new_contact').validate({
    ignore: ":hidden",
    rules: {
      'contact[name]': {
        required: true
      },
      'contact[email]': {
        required: true,
        email: true
      },
      'contact[telephone]': {
        required: true,
        phoneUS: true
      },
      'contact[description]': {
        required: true
      }
    },

    messages: {
      'contact[name]': {
        required: "Please provide a name."
      },
      'contact[telephone]': {
        required: "Please provide a valid telephone number."
      },
      'contact[description]': {
        required: "Please provide a description of the work you would like done."
      }
    },

    errorPlacement: function(error, element) {
      error.appendTo("#error");
    }

  });
});
  • 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-17T16:48:57+00:00Added an answer on June 17, 2026 at 4:48 pm

    You can use the ignore attribute in the plugin

    $("#new_contact").validate({
      //All your options go here
      ignore: ':hidden'
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Put them all in one separate folder structure or along with classes which implements
I'm very new to jQuery and I'm writing some validation logic on a form.
How do i (using jquery validation) put all the messages in a div tag
I have contact list which contains (checkbox,firstname,lastname,phone). I am creating this list using html
I have a question about where to put the logic between jquery and php.
I am currently banging my head where to put a common logic for some
I think this is a fairly common question: how to put my business logic
In an MVC web app, where is the right place to put the code/logic
We usually put unnecessary checks in our business logic to avoid failures. Eg. 1.
Following is my php login script, There are one problem. If i put any

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.