我已在RangeValidator
上应用了TextBox
。但它总是显示错误:无效范围,虽然我给出了最小值10和最大值25.我希望用户不能输入长度小于10且大于25的值。我希望用户可以输入任何内容,所以我在type="string"
中有RangeValidator
。但它总是给我错误信息:无效范围。
<td>
<asp:TextBox ID="tbPassword" runat="server" MaxLength="25" type="password">
</asp:TextBox>
<asp:RequiredFieldValidator ID="rfvPassword" runat="server"
ControlToValidate="tbPassword" ForeColor="red" Display="Dynamic"
ErrorMessage="Password is required." SetFocusOnError="true">
</asp:RequiredFieldValidator>
<asp:RangeValidator ID="rvPassword" ControlToValidate="tbPassword"
ForeColor="red" Display="Dynamic" MinimumValue="10" MaximumValue="25"
SetFocusOnError="true" Type="String" runat="server"
ErrorMessage="Invalid Range">
</asp:RangeValidator>
</td>
答案 0 :(得分:5)
为此,您需要使用Emad Mokhtar建议的CustomValidator
控件。
对于服务器端验证,请创建一个这样的事件。
protected void TextValidate(object source, ServerValidateEventArgs e)
{
e.IsValid = (e.Value.Length >= 10 && e.value.Length <= 25);
}
对于客户端验证,请创建一个这样的javascript函数。
<script type="text/javascript">
function validateLength(oSrc, args){
args.IsValid = (args.Value.length >= 10 && args.Value.length <= 25);
}
</script>
然后在你的aspx标记中有这样的CustomValidator
控件。
<asp:Textbox id="tbPassword" runat="server" text=""></asp:Textbox>
<asp:CustomValidator id="customValidator" runat="server"
ControlToValidate = "tbPassword"
OnServerValidate="TextValidate"
ErrorMessage = "Password must be between 10 to 25 characters!"
ClientValidationFunction="validateLength" >
</asp:CustomValidator>
您可以找到更多详情here。
答案 1 :(得分:1)
可以使用CustomValidator Control实现此验证并应用客户端和服务器端验证,请查找示例here。
答案 2 :(得分:0)
我最近观察到这个很酷的功能,只需使用以下属性来控制asp / html。 的minLength =&#34; 10&#34;最大长度=&#34; 1000&#34;
因为属性清楚地表明它允许最少10个字符,最多允许1000个字符。