jQuery 验证规则和消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14179417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery Validation rules and messages
提问by Kim Hin
I'm having trouble setting ups the rules and messages for jQuery Validation.
我在为 jQuery 验证设置规则和消息时遇到问题。
I'm trying to get the value from resume[zip_code], but I don't know how to write it in JQuery. I tried
我正在尝试从 resume[zip_code] 中获取值,但我不知道如何在 JQuery 中编写它。我试过
rules: {
resume[zip_code]: required: true
}
but does not work.
但不起作用。
This is where I need to get the values from.
这是我需要从中获取值的地方。
<div class='control-group'><label class='control-label'>Zip Code: </label>
<div class='controls'>
<input class="required" type='text' name='resume[zip_code]'>
</div>
</div>
Application code
应用代码
post '/create_resume' do
session[:errors].clear
@user = User.first(:email_address => session['user'])
@resume = @user.resumes.new(params[:resume])
@resume.save
params[:education].each_key do |school|
@school = @resume.educations.new(params[:education][school]
@school.save
end
params[:job].each_key do |job|
@job = @resume.jobs.new(params[:job][job])
@job.save
end
params[:otherskill].each_key do |other|
@otherskill = @resume.otherskills.new(params[:otherskill][other])
@otherskill.save
end
if session[:errors].empty? === false
erb :resume_form
else
session[:resume_output].clear
erb :resume_output
end
end
jQuery
jQuery
I know what the layout is but just don't know how to retrieve the values
我知道布局是什么,但只是不知道如何检索值
$(#myForm).validate({
rules: {
something: key => value
},
messages: {
something: "value"
}
});
回答by Sparky
I have no idea how to write your rules since you never stated which rules you wanted. Using only your provided HTML, this is how you'd set up rules and messages.
我不知道如何编写您的规则,因为您从未说明过您想要的规则。仅使用您提供的 HTML,这就是您设置规则和消息的方式。
To demonstrate usage, I am arbitrarilymaking the field required
with a minimum length of 5 numbers.
为了演示用法,我任意制作required
了最小长度为 5 个数字的字段。
Edit and add your own rules accordingly.
相应地编辑和添加您自己的规则。
$('#myForm').validate({
rules: {
"resume[zip_code]": {
required: true,
minlength: 5,
digits: true
}
},
messages: {
"resume[zip_code]": {
required: "this field is required",
minlength: "this field must contain at least {0} characters",
digits: "this field can only contain numbers"
}
}
});
You also do not need to put class="required"
within the HTML because this rule is already specified with your jQuery .validate()
method.
您也不需要放置class="required"
在 HTML 中,因为此规则已在您的 jQuery.validate()
方法中指定。
<input type='text' name='resume[zip_code]' />
Working Demo:
工作演示:
See documentation for all methods and rules:
有关所有方法和规则,请参阅文档:
http://docs.jquery.com/Plugins/Validation
http://docs.jquery.com/Plugins/Validation
If you want to validate the field as a required US Zip Code, simply use the zipcodeUS
rule, but you must first include the "Additional Methods" file found here.
如果您想将该字段验证为必需的美国邮政编码,只需使用该zipcodeUS
规则,但您必须首先包含此处的“其他方法”文件。
http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/additional-methods.js
http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/additional-methods.js
jQuery:
jQuery:
rules: {
"resume[zip_code]": {
required: true,
zipcodeUS: true
}
},
messages: {
"resume[zip_code]": {
required: "this field is required",
zipcodeUS: "this field must contain valid US zip code"
}
}