如何基于哪一个可见来验证字段?

时间:2013-01-20 05:36:57

标签: jquery ruby-on-rails-3 jquery-validate

我在哪里放置JQuery Validation插件的逻辑来根据哪一个可见来验证字段?

<%= 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");
    }

  });
});

2 个答案:

答案 0 :(得分:1)

忽略不可见的输入。

$("form").validate({ignore:":not(:visible)"});

答案 1 :(得分:1)

您可以在插件中使用ignore属性

$("#new_contact").validate({
  //All your options go here
  ignore: ':hidden'
});