[英]Rails : jQuery validation plug-in not working?
I'm a beginner coder in Ruby and currently I'm trying to use this plugin: 我是Ruby的初学者,目前正在尝试使用此插件:
Currently however, the javaScript validation code that I have written to validate the name, email, and phone number fields in my simple rails form is not working. 但是,目前,我编写的用于验证我的简单rails表单中的名称,电子邮件和电话号码字段的javaScript验证代码不起作用。 I can enter anything or not anything into any of those fields and it will submit (which is not what I want to occur). 我可以在这些字段中输入任何内容,也可以不输入任何内容,它将提交(这不是我想要的)。
Here's the code in my /app/assets/javascripts/user.js.coffee file: 这是我的/app/assets/javascripts/user.js.coffee文件中的代码:
$("#theform").validate({
rules: {
name: {
required:true,
minlength:2,
maxlength:50
},
email: {
required: true,
email: true
minlength:2,
maxlength:50
},
phone_number: {
required: true,
phoneUS: true,
minlength:9,
maxlength:20
}
}
messages: {
name: {
required: "Please provide your name",
minlength: jquery.format("At least {0} characters required!"),
maxlength: "Too many characters!"
},
email: {
required: "Please provide your email",
email: "Your email address must be in the format of name@domain.com",
minlength: jquery.format("At least {0} characters required!"),
maxlength: "Too many characters!"
},
phone_number: {
required: "Please provide your phone number",
phoneUS: "Please provide a valid US phone number",
minlength: jquery.format("At least {0} characters required!"),
maxlength: "Too many characters!"
}
}
});
Here's the code in my /app/views/layouts/_form.html.erb partial (the actual html file which renders this partial doesn't have much in it since it's a simple ajax submit form): 这是我的/app/views/layouts/_form.html.erb部分中的代码(呈现此部分的实际html文件没有太多内容,因为这是一个简单的ajax提交表单):
<div id="theform">
<%= form_for(@user, :remote => true) do |f| %>
Name: <div id='name'><%= f.text_field :name %></div><br/>
Email: <div id='email'><%= f.text_field :email %></div><br/>
Phone Number: <div id='phone_number'><%= f.text_field :phone_number%></div><br/>
<%= f.submit "Submit" %>
<% end %>
</div>
Here's the application.html.erb layout file for this form: 这是此表单的application.html.erb布局文件:
<!DOCTYPE html>
<html>
<head>
<title>IntroProject</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= javascript_include_tag "jquery-1.7.2.min" %>
<%= javascript_include_tag "jquery-validation-1.9.0" %>
<%= csrf_meta_tags %>
</head>
<body>
<h1><%= image_tag("leaf_logo.png", alt:"Leaf") %> Sample Form </h1>
<div class="container">
<%= yield %>
</div>
<br/>
Try this: 尝试这个:
<%= form_for(@user, :remote => true, :form_to_validate => 'user') do |f| %>
And 和
$('form[form_to_validate=user]').validate({
...
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.