2

我使用路径 assets\javascripts\jquery.validate.js (rails 3.2) 上的 jquery 插件进行客户端验证。但它不起作用。我的表格:

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


10 %> “提交” %>

我的 application.js 文件:

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

});

怎么了?

UPD 1 生成的 HTML- 代码

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

2 回答 2

1

假设您正在验证这样的测试输入

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

试试这个

$("#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'
         }
      }
    });
于 2012-11-26T10:21:35.520 回答
0

解决方案似乎是将名称值放在引号中。

$("#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'
     }
  }
});

请参阅 Ruby on Rails 3 上的 jQuery Validator 插件?详情。

于 2014-01-17T18:08:23.187 回答