twitter-bootstrap 如何在 Bootstrap 中为输入字段指定自定义错误消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14926438/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 16:24:12  来源:igfitidea点击:

How do you specify a custom error message in Bootstrap for input fields

twitter-bootstrapvalidation

提问by mokumaxCraig

I'm using Twitter-Bootstrap in .NET 4.0. I'm not using any additional jquery validation plugins.

我在 .NET 4.0 中使用 Twitter-Bootstrap。我没有使用任何额外的 jquery 验证插件。

I have this code:

我有这个代码:

<asp:TextBox ID="Url" runat="server" required  Text=''  placeholder="enter the  URL" pattern="http://*"  messages="Must start with 'http://' or 'https://' "></asp:TextBox>

The Regex validation works if I type 'asdf' in:

如果我在以下位置键入“asdf”,则正则表达式验证有效:

enter image description here

在此处输入图片说明

My Question is: how do I customize the "Please match the requested format" error to say "Must start with 'http://' or 'https://'"?

我的问题是:如何自定义“请匹配请求的格式”错误以说“必须以‘http://’或‘https://’开头”?

As you can see I've tried "messages=". I've also tried several others.

如您所见,我尝试过“消息=”。我也试过其他几个。

  1. How do I customize the generic error message?
  2. Is there any documentation anywhere for Bootstrap that has more info on this?
  1. 如何自定义通用错误消息?
  2. 是否有任何有关 Bootstrap 的文档提供更多信息?

回答by Zaid Crouch

This actually has nothing to do with bootstrap, rather this the implementation of html5 form input validation in your browser (Chrome, by the look of your screenshot).

这实际上与引导程序无关,而是在您的浏览器中实现 html5 表单输入验证(Chrome,通过您的屏幕截图)。

When using the patternattribute, you should use the titleto provide additional help to the user (see the MDN docs).

使用该pattern属性时,您应该使用 为title用户提供额外帮助(请参阅 MDN 文档)。

With title="Must start with 'http://' or 'https://'", you should see something like (in Chrome, different browsers will render this differently):

使用title="Must start with 'http://' or 'https://'",您应该会看到类似的内容(在 Chrome 中,不同的浏览器会以不同的方式呈现):

Input validation using the pattern and title attributes

使用模式和标题属性进行输入验证

You could also try changing the input type to URL, which would give you a different message even without the title, but my own experience has been that sometimes the browser implemented validation can be a little frustrating.

您也可以尝试将输入类型更改为 URL,即使没有标题,这也会为您提供不同的消息,但我自己的经验是,有时浏览器实现的验证可能有点令人沮丧。

回答by Pedro Rom?o

if you want to change invalid message you can use oninvalid attribute.

如果要更改无效消息,可以使用 oninvalid 属性。

oninvalid="this.setCustomValidity('User ID is a must')"

oninvalid="this.setCustomValidity('User ID is a must')"

hope this helps you.

希望这对你有帮助。