我正在尝试使用parsley.js验证一个简单的表单,我在parsley.js.上非常初学。我想在一个自定义验证方法中使用window.ParsleyValidator.addValidator()方法显示多个错误消息。所以我已经试过了。
我创建的简单html表单
<html>
<head>Custom Validation Parsley
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="js/parsley.min.js"></script>
<script src="js/my-validator-parsley.js"></script>
</head>
<body>
<form class="cmxform" id="commentForm" method="get" action="">
<fieldset>
<legend>Please provide your name, email address (won't be published) and a comment</legend>
<p>
<label for="cemail">E-Mail with custom method (required)</label>
<input id="cemail" name="checkEmail" data-parsley-checkEmail>
</p>
<p>
<input class="submit" type="submit" value="Submit">
</p>
</fieldset>
</form>
<script>
$("#commentForm").parsley();
</script>
</body>
</html>
包含自定义验证方法的javascript文件
var errorMsg = "";
window.ParsleyValidator.addValidator('checkEmail', function(value) {
console.log(value);
if (value == "") {
errorMsg = "this field must not be empty";
return false;
} else {
var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (!regex.test(value)) {
errorMsg = "this field must be email ***";
}
return regex.test(value);
window.ParsleyValidator.addMessage('en', 'checkEmail',
errorMsg);
}
}, 32)
//.addMessage('en','checkEmail',window.ParsleyValidator.catalog)
;
但这对我不起作用。任何人都可以就此提出建议以及如何处理这项任务?
答案 0 :(得分:4)
您只能在给定的区域设置中为每个约束分配一条消息。
来自http://parsleyjs.org/doc/annotated-source/validator.html:
<input type="email" required />
因此,如果您想要多条消息,请定义多个验证器。
另外,
如果你想要一个字段,那么就不需要检查它是否为空;只需为其添加window.ParsleyUI.updateError(parsleyInstance, name, message);
属性(例如catalog
)
Parsley捆绑了常见的验证器(http://parsleyjs.org/doc/index.html#validators)和&#34;电子邮件&#34;是其中之一;要启用它,请在您的输入中添加属性addMessage()
(例如window.ParsleyValidator.addValidator('checkEmail', function(value) {
if (...)
window.ParsleyValidator.catalog.en.checkemail = 'Some error message AAA';
else
window.ParsleyValidator.catalog.en.checkemail = 'Some error message BBB';
return (...);
}, 32);
)
更新:
来自http://parsleyjs.org/doc/index.html#psly-ui-for-javascript:
create create README.rdoc create Rakefile create config.ru create .gitignore create Gemfile create app create app/assets/javascripts/application.js create ----- Some stuff here ------ create ----- Some stuff here ------ create ----- Some stuff here ------ create vendor/assets/javascripts/.keep create vendor/assets/stylesheets create vendor/assets/stylesheets/.keep run bundle install Fetching gem metadata from https://rubygems.org/............ Fetching version metadata from https://rubygems.org/... Fetching dependency metadata from https://rubygems.org/.. Resolving dependencies... Using rake 10.4.2 Using i18n 0.7.0 Using .... some stuff here ... Using .... some stuff here ... Using .... some stuff here ... Using coffee-rails 4.1.0 Using multi_json 1.11.0 Using jbuilder 2.2.13 Using jquery-rails 4.0.3 Errno::EACCES: Permission denied @ rb_sysopen - /home/ayush/.rvm/gems/ruby-2.2.1/gems/mysql2-0.3.18/README.md An error occurred while installing mysql2 (0.3.18), and Bundler cannot continue. Make sure that `gem install mysql2 -v '0.3.18'` succeeds before bundling. run bundle exec spring binstub --all /home/ayush/.rvm/gems/ruby-2.2.1@global/gems/bundler-1.8.4/lib/bundler/resolver.rb:369:in `resolve': Could not find gem 'mysql2 (>= 0) ruby' in the gems available on this machine. (Bundler::GemNotFound) from /home/ayush/.rvm/gems/ruby-2.2.1@global/gems/bundler-1.8.4/lib/bundler/resolver.rb:167:in `start' from /home/ayush/.rvm/gems/ruby-2.2.1@global/gems/bundler-1.8.4/lib/bundler/resolver.rb:129:in `resolve' from /home/ayush/.rvm/gems/ruby-2.2.1@global/gems/bundler-1.8.4/lib/bundler/definition.rb:193:in `resolve' from /home/ayush/.rvm/gems/ruby-2.2.1@global/gems/bundler-1.8.4/lib/bundler/definition.rb:132:in `specs' from /home/ayush/.rvm/gems/ruby-2.2.1@global/gems/bundler-1.8.4/lib/bundler/definition.rb:177:in `specs_for' from /home/ayush/.rvm/gems/ruby-2.2.1@global/gems/bundler-1.8.4/lib/bundler/definition.rb:166:in `requested_specs' from /home/ayush/.rvm/gems/ruby-2.2.1@global/gems/bundler-1.8.4/lib/bundler/environment.rb:18:in `requested_specs' from /home/ayush/.rvm/gems/ruby-2.2.1@global/gems/bundler-1.8.4/lib/bundler/runtime.rb:13:in `setup' from /home/ayush/.rvm/gems/ruby-2.2.1@global/gems/bundler-1.8.4/lib/bundler.rb:122:in `setup' from /home/ayush/.rvm/gems/ruby-2.2.1@global/gems/bundler-1.8.4/lib/bundler/setup.rb:18:in `<top (required)>' from /home/ayush/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require' from /home/ayush/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
手动编辑错误消息。
此外,您可以通过直接将其分配给~/.guard.rb
而不是使用# Guardfile
notification :tmux,
display_message: true,
timeout: 5, # in seconds
default_message_format: '%s >> %s',
# the first %s will show the title, the second the message
# Alternately you can also configure *success_message_format*,
# *pending_message_format*, *failed_message_format*
line_separator: ' > ', # since we are single line we need a separator
color_location: 'status-left-bg', # to customize which tmux element will change color
# Other options:
default_message_color: 'black',
success: 'colour150',
failure: 'colour174',
pending: 'colour179',
# Notify on all tmux clients
display_on_all_clients: false,
color_location: %w[status-left-bg pane-active-border-fg pane-border-fg]
来设置来自验证程序函数的消息(请注意验证程序的名称应全部为小写):
public boolean is_touched() {
if (Gdx.input.justTouched()) {
float xx = Gdx.input.getX();
float yy = Gdx.input.getY();
float x = position.x;
float y = position.y;
return (xx - x) * (xx - x) + (yy - y) * (yy - y) < radius * radius;
}
}