客户端验证jquery插件不起作用

时间:2012-11-26 09:46:22

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

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

     update_name_phone_city_user_registration_path,       :html => {:id => “form_edit_name”,:method => :put},:remote =>是)do | f | %GT;             
                         
                         
             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>

2 个答案:

答案 0 :(得分: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'
         }
      }
    });

答案 1 :(得分: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'
     }
  }
});

请参阅 jQuery Validator Plugin on Ruby on Rails 3?了解详情。