2

I use for client validation the jquery plugin on the path assets\javascripts\jquery.validate.js (rails 3.2). But it doesn't work. My form:

update_name_phone_city_user_registration_path, :html => { :id => "form_edit_name", :method => :put }, :remote => true) do |f| %>


10 %> "submit" %>

My application.js file:

//= require jquery
//= require jquery_ujs
//= require bootstrap-modal
//= require_tree .


$(document).ready(function() {

$("#form_edit_name").validate({
   debug: true,
   rules: {
      "user[name]": {
        required: true,
        minlength: 2
      },
    "user[name]": {
      required: 'Your username is required',
      minlength: 'The minimum number of characters is 2'
     }

     alert("ok");
   });

});

What's wrong?

UPD 1 The generated HTML- code

<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-modal.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery.validate.js?body=1" type="text/javascript"></script>
<script src="/assets/application.js?body=1" type="text/javascript"></script>


<form accept-charset="UTF-8" action="/users" class="edit_user" id="form_edit_name" method="post">
<div><label for="user_name">Name</label><br />
        <input id="user_name" name="user[name]" size="30" type="text" value="" /></div>
</form>

2 Answers 2

1

assuming you are validating a test input like this

<input type="text" id="name" name="name" />

try this

$("#form_edit_name").validate({
       debug: true,
       rules: {
          "name": { required: true, minlength: 2 },
       messages: {
        "name": {
          required: 'Your username is required',
          minlength: 'The minimum number of characters is 2'
         }
      }
    });
Sign up to request clarification or add additional context in comments.

4 Comments

@Stack Stack what does your html look like?
I doubt having a name with square brackets in will work with jquery.
I think, problem in modal window.
I loaded my form in modal via ajax. After removing form out of modal window, all works.
0

The solution seems to be placing the name value in the quotes.

$("#form_edit_name").validate({
   debug: true,
   rules: {
      "user[name]": { required: true, minlength: 2 },
   messages: {
      "user[name]": {
       required: 'Your username is required',
       minlength: 'The minimum number of characters is 2'
     }
  }
});

See jQuery Validator Plugin on Ruby on Rails 3? for details.

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.