1

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...

3 Answers 3

6

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
Sign up to request clarification or add additional context in comments.

1 Comment

not a good idea to use gem for js plugin.. we can simply add any plugin without any gem
3

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();  
}); 

2 Comments

Thats great ..but how to do it for email,mobile,password,password confirmation.As they require the name field to be same as in javascript
You can give email validation as in class field itself. like <%= f.text_field :firstname, :class => "required email" %>
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.