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

时间:2013-02-17 21:45:40

标签: twitter-bootstrap validation

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

我有这段代码:

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

如果我在:

中键入'asdf',则正则表达式验证有效

enter image description here

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

正如你所看到的,我尝试过“messages =”。我也尝试了其他几个。

  1. 如何自定义常规错误消息?
  2. Bootstrap的任何文档是否都有关于此的更多信息?

2 个答案:

答案 0 :(得分:10)

这实际上与bootstrap无关,而是在浏览器中实现html5表单输入验证(Chrome,通过截图的外观)。

使用pattern属性时,您应使用title为用户提供其他帮助(see the MDN docs)。

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

Input validation using the pattern and title attributes

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

答案 1 :(得分:5)

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

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

希望这会对你有所帮助。