HTML5“必需"asp:DropDownList上的属性 [英] HTML5 "required" attribute on asp:DropDownList
问题描述
假设我的页面上有这个DDL:
Let's say I have this DDL on my page:
<asp:DropDownList ID="senha" runat="server" required="required" CssClass="form-control">
<asp:ListItem Value="" Text="empty" Selected="True"></asp:ListItem>
<asp:ListItem Value="1" Text="test1"></asp:ListItem>
<asp:ListItem Value="2" Text="test2"></asp:ListItem>
</asp:DropDownList>
在此示例中,必填字段将毫无问题地工作,如果您不选择一个值,则它将阻止提交.
但是,如果初始值为0,该怎么办?
In this one, the required field will work without problem, if you don't select a value, then it will block the submit.
But what if the initial value is 0?
<asp:DropDownList ID="senha" runat="server" required="required" CssClass="form-control">
<asp:ListItem Value="0" Text="empty" Selected="True"></asp:ListItem>
<asp:ListItem Value="1" Text="test1"></asp:ListItem>
<asp:ListItem Value="2" Text="test2"></asp:ListItem>
</asp:DropDownList>
现在必需"属性不再起作用.
我想做的是在值中放置一个模式,以仅接受大于零的值,但是我对RegExp并不熟悉,我也不知道HTML模式"属性是否可以满足我的要求.
有人可以启发我吗?
我宁愿使用纯HTML的解决方案,但如果只需要最少的编程,那我就别无选择...
Now the "required" attribute doesn't work anymore.
What I want to do is put a pattern in the value to only accept values greater than zero, but I'm not familiar with RegExp, and I don't know if the HTML "pattern" attribute will do what I want.
Can someone enlighten me?
I would rather use a solution that uses pure HTML, but if a minimal programming is required, then I don't have choice...
推荐答案
If you just want to write mark up the RequiredFieldValidator should work
<asp:DropDownList ID="senha" runat="server" required="required" CssClass="form-control">
<asp:ListItem Value="0" Text="empty" Selected="True"></asp:ListItem>
<asp:ListItem Value="1" Text="test1"></asp:ListItem>
<asp:ListItem Value="2" Text="test2"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="reqSenha" runat="server" SetFocusOnError="true" InitialValue="0" ErrorMessage="*" ControlToValidate="senha" />
这篇关于HTML5“必需"asp:DropDownList上的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!