Mohammad Sadiq Shaikh
Mohammad Sadiq Shaikh

Reputation: 3200

How to use jquery validation plugin in rails

How to use jquery validation plugin for the rails form given below.

I want complete steps about how to make the it for different fields.

<%= form_for(@user) do |f| %>
 <div class="field">
  <%= f.label :firstname %><br />
  <%= f.text_field :firstname %>
</div>
<div class="field">
  <%= f.label :lastname %><br />
 <%= f.text_field :lastname %>
</div>
<div class="field">
  <%= f.label :mobile %><br />
  <%= f.text_field :mobile %>
</div>
<div class="field">
  <%= f.label :email %><br />
  <%= f.text_field :email %>
</div>
<div class="actions">
  <%= f.submit %>
</div>
<% end %>

plz help me...

Upvotes: 1

Views: 10806

Answers (3)

Sigu Magwa
Sigu Magwa

Reputation: 568

You can use the jquery validation rails gem based on jquery validation plugin. To use it

  1. Add the gem to your gemfile gem 'jquery-validation-rails'
  2. On your app/assets/javascripts/application.js add the following lines

    //= require jquery.validate.additional-methods //= require jquery.validate

  3. As of turbolinks 5 which is included in rails 5 instead of using $(document).ready use $(document).on "turbolinks:load" as described in turbolinks readme

Upvotes: 6

Mohammad Sadiq Shaikh
Mohammad Sadiq Shaikh

Reputation: 3200

Got the solution to add range ,min ,max etc

we have to use

   <%= f.text_field :mobile,:class => "required number",:minlength=>"10",
   :maxlength=>"10"%>

the above for mobile

Upvotes: 0

Raji
Raji

Reputation: 845

First include the jquery validation file in your layout. Then in rails form add class to validation required fields as <%= f.text_field :firstname, :class => "required" %>. Then write a method with form_id to trigger the validation. That is you have to give like

$(document).ready(function () { 
   $("your_form_id").validate();  
}); 

Upvotes: 3

Related Questions