简体   繁体   English

jQuery 验证 - 如何显示错误的默认标注?

[英]jQuery Validation - How to display the default callout on errors?

If I create an input control like this:如果我创建这样的输入控件:

<input type="number">

And type letters in it, an error will be displayed in a popup-like control.并在其中键入字母,错误将显示在弹出式控件中。 I've seen it referred to as a callout but I'm not sure if that's the right name.我已经看到它被称为标注,但我不确定这是否是正确的名称。 It looks like this:它看起来像这样:

在此处输入图片说明

But if I define my own rules in either the jQuery Validation Plugin or in ASP.NET then there's no fancy popup anymore, just a label which is appended.但是如果我在jQuery Validation Plugin或 ASP.NET 中定义我自己的规则,那么就没有花哨的弹出窗口了,只是附加了一个标签。 It doesn't look nearly as good and it isn't very consistent with the above either.它看起来几乎没有那么好,并且与上述内容也不太一致。

Is there a simple way to make my custom error messages show up in the same way?有没有一种简单的方法可以让我的自定义错误消息以相同的方式显示?

Your picture shows the default HTML5 validation which is rendered ( or not rendered ) depending solely on the specific browser version.您的图片显示了仅根据特定浏览器版本呈现(或不呈现)的默认 HTML5 验证。

http://i.stack.imgur.com/ZGi5z.jpg

The browser must support HTML5 validation in order to see these pop-ups and Chrome's pop-ups will look different than Firefox's, etc. HTML5 validation is configured using HTML5 validation attributes that you'd add inline to each input element like this...浏览器必须支持 HTML5 验证才能看到这些弹出窗口,Chrome 的弹出窗口看起来与 Firefox 等不同。HTML5 验证是使用HTML5 验证属性配置的,您可以像这样向每个input元素内联添加这些属性...

<input type="number" required="required" max="5" ....

However, when you employ the jQuery Validate plugin , it dynamically adds the novalidate attribute to the <form> tag...但是,当您使用jQuery Validate 插件时,它会动态地将novalidate属性添加到<form>标记...

<form novalidate="novalidate" id="myform" ....

The novalidate attribute is used to disable the HTML5 validation so that jQuery Validate can take over all form validation without interference from HTML5. novalidate属性用于禁用HTML5 验证,以便 jQuery Validate 可以在不受 HTML5 干扰的情况下接管所有表单验证。 Yes, the default jQuery Validate labels are bland, but that's the "blank canvas" which allows design flexibility and cross-browser consistency and reliability.是的,默认的 jQuery Validate 标签是乏味的,但这是“空白画布”,它允许设计灵活性和跨浏览器的一致性和可靠性。 See below.见下文。


If you want error messages that look more like the pop-ups you'd get with HTML5, then you can create your own with CSS/jQuery.如果您希望错误消息看起来更像您使用 HTML5 获得的弹出窗口,那么您可以使用 CSS/jQuery 创建自己的错误消息。

Quote:引用:

"Is there a simple way to make my custom error messages show up in the same way?" “有没有一种简单的方法可以让我的自定义错误消息以同样的方式显示?”

That depends on if you think adding another jQuery plugin is simple.这取决于您是否认为添加另一个 jQuery 插件很简单。 You could integrate a jQuery plugin like qTip2 or ToolTipster into jQuery Validate.您可以将qTip2ToolTipster等 jQuery 插件集成到 jQuery Validate 中。 These plugins have many options, themes, and CSS that you can customize to no end.这些插件有许多选项、主题和 CSS,您可以无限自定义。 It can be tricky though as each plugin gets integrated differently depending on how it works, its manipulation methods, callbacks, etc.但是这可能很棘手,因为每个插件的集成方式都不同,具体取决于其工作方式、操作方法、回调等。

Here's a working demo showing how ToolTipster v3 (default - no theme) was easily integrated into jQuery Validate:这是一个演示如何将 ToolTipster v3(默认 - 无主题)轻松集成到 jQuery Validate 中:

http://jsfiddle.net/2DUX2/3/ http://jsfiddle.net/2DUX2/3/

在此处输入图片说明

Source : How to display messages from jQuery Validate plugin inside of Tooltipster tooltips?来源如何在 Tooltipster 工具提示中显示来自 jQuery Validate 插件的消息?

This is what you are talking is HTML5 feature use required attribute with your form element这就是您所说的 HTML5 功能在您的表单元素中使用required属性

<input required>

and it will show you default error message but if you want to show custom error message than use title attribute它会向您显示默认错误消息,但如果您想显示自定义错误消息而不是使用title属性

<input required title="Custom error message">

Check this URL for more information http://demosthenes.info/blog/848/Create-Custom-HTML5-Form-Error-Messages-with-the-Title-Attribute检查此 URL 以获取更多信息http://demosthenes.info/blog/848/Create-Custom-HTML5-Form-Error-Messages-with-the-Title-Attribute

Hope it will answer your question希望它能回答你的问题

That 'callout' as you refer to it is actually chromes way of interpreting the spec, the error message will look different or not pop up at all in some browsers您所指的“标注”实际上是解释规范的 chromes 方式,错误消息在某些浏览器中看起来会有所不同或根本不弹出

stares at IE盯着 IE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM