Rob Johnston
Rob Johnston

Reputation: 879

ASP.NET WebForms Textbox removes HTML5 pattern attribute

Let's say that I have a textbox in a webforms project that looks like this:

<asp:textbox id="TextBox1" runat="server" pattern="[A-Za-z0-9\s]{4,}"/>

Upon rendering, the pattern attribute is removed. If I add the attribute in the code-behind the attribute is also removed:

TextBox1.Attributes.Add("pattern", "[A-Za-z0-9\\s]{4,}");

It doesn't matter what the pattern actually is. It's the attribute named "pattern" that is scrubbed-out upon rendering. Name the attribute something else and it will be rendered.

I can find no documentation about this and nobody with the same problem. Has anybody got a work-around, short of writing a custom server control that extends TextBox?

Upvotes: 1

Views: 3249

Answers (2)

Rob Johnston
Rob Johnston

Reputation: 879

I've tracked it down to the use of the Web Experience Toolkit. Specifically, the part that reads, "Remove the pattern attribute until it is safe to use with jQuery Validation". Will file an issue with them.

https://github.com/wet-boew/wet-boew/blob/0f614e0efd50df7970a6fc6bb72104e43b48e8f5/src/plugins/formvalid/formvalid.js#L105

Upvotes: 1

Gregg
Gregg

Reputation: 625

Your patter attribute is not being removed. If you define your pattern as the pattern attribute of the textbox:

   <asp:textbox id="TextBox1" runat="server" pattern="[A-Za-z0-9\s]{4,}"/>

Then in your code behind you can check the value:

     var r = TextBox1.Attributes["pattern"];

Upvotes: 1

Related Questions